-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegex validate PIN code.js
More file actions
23 lines (18 loc) · 927 Bytes
/
Regex validate PIN code.js
File metadata and controls
23 lines (18 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// ATM machines allow 4 or 6 digit PIN codes and PIN codes cannot contain anything but exactly 4 digits or exactly 6 digits.
// If the function is passed a valid PIN string, return true, else return false.
// eg:
// validatePIN("1234") === true
// validatePIN("12345") === false
// validatePIN("a234") === false
function validatePIN(pin) {
if (pin && (pin.length == 4 || pin.length == 6)) {
const match = pin.match(/[0-9]/g)
if (match) return pin == match.join('')
} else return false
}
console.log(validatePIN('8572'), true, "Wrong output for '1234567'")
console.log(validatePIN('1234567'), false, "Wrong output for '1234567'")
console.log(validatePIN('-1234'), false, "Wrong output for '-1234'")
console.log(validatePIN('1.234'), false, "Wrong output for '1.234'")
console.log(validatePIN('-1.234'), false, "Wrong output for '-1.234'")
console.log(validatePIN('09876'), false, "Wrong output for '00000000'")