Bạn có thể xác nhận ghim dựa trên độ dài và loại ghim phải là chuỗi, v.v.
Ví dụ
Sau đây là mã -
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") Để chạy chương trình trên, bạn cần sử dụng lệnh sau -
node fileName.js.
Đây, tên tệp của tôi là demo254.js.
Đầu ra
Điều này sẽ tạo ra kết quả sau trên bảng điều khiển -
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