您可以根据长度和类型来验证引脚,引脚类型必须是字符串等。
以下是代码-
function simpleValidationForPin(pinValues) { if (!(typeof pinValues === "string" && !~pinValues.indexOf('.') && !isNaN(Number(pinValues)) && (pinValues.length === 2 || pinValues.length === 4))) { return false; } else { return true; } } if (simpleValidationForPin("0000.00") == true) console.log("This is a valid pin") else console.log("This is not a valid pin") if (simpleValidationForPin(66) == true) console.log("This is a valid pin") else console.log("This is not valid pin") if (simpleValidationForPin("4444") == true) console.log("This is a valid pin") else console.log("This is not a valid pin") if (simpleValidationForPin("66") == true) console.log("This is a valid pin") else console.log("This is not valid pin") if (simpleValidationForPin("666") == true) console.log("This is a valid pin") else console.log("This is not a valid pin")
要运行上述程序,您需要使用以下命令-
node fileName.js.
在这里,我的文件名为demo254.js。
输出结果
这将在控制台上产生以下输出-
PS C:\Users\Amit\javascript-code> node demo254.js This is not a valid pin This is not a valid pin This is a valid pin This is a valid pin This is not a valid pin