JavaScript 设置密码验证
你可以根据长度和针的类型来验证针,必须是字符串等。
示例
以下是代码 –
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