Last active
July 21, 2019 06:03
-
-
Save Aramics/fa64fa587e69b00ced3d9f15cdbb9ac3 to your computer and use it in GitHub Desktop.
Snippet to Validate a Pin code or Password for repetition, matched length and weak pin codes ( nested integers)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* | |
* Author: aramics | |
* Function to validate pin code or password | |
* Return boolean; | |
* @param pin string to validate as pin e.g "9358" | |
* @param norepeat boolean: check if pin contain repetitive number | |
* @param noserial boolean: check if pin contain nexted integer e.g 12 or 34 | |
* @param pinLength int: length of the pin | |
* validatePin("9358",4,false,true) | |
*/ | |
function validatePin(pin,pinLength=4,norepeat=true,noserial=true){ | |
if (pin == null || pin.length != pinLength) | |
return false; | |
var count = Array(10); | |
for (var i = 0; i < pin.length; i++) { | |
var c = pin.charAt(i); | |
c = parseInt(c); | |
if(isNaN(c)) | |
return false; | |
if(norepeat && count.indexOf(c)>-1){ //duplicate number | |
return false; | |
} | |
if(noserial && c+1 == pin.charAt(i+1)) //increment e.g 1234 | |
return false | |
count[i] = c; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment