You have to verify a password to confirm that it meets the following conditions:
1. At least 6 characters in length
2. At least one uppercase letter
3. At least one Lowercase letters
4. At least one number
5. No other special characters except 2, 3, and 4 points, that is, only letters and numbers
For this type of verification problem, regular expressions are undoubtedly the first choice, but if regular expressions are not used, it is also possible to write verification logic.
For this problem, we divide it into two tests:
According to the first requirement, establish a length detection.
var lengthValid = function(pass){ return pass.length >= 6; };
Based on points 2, 3, 4, and 5, establish a content detection function.
The logic is as follows: count the number of uppercase and lowercase letters and numbers in the password string. If special symbols are encountered, false is returned directly.
var contentValid = function(pass){ var lowerNum = 0; var upperNum = 0; var numNum = 0; for(var i=0;i<pass.length;i++){ var code = pass.charCodeAt(i); if(code >= 48 && code <= 57){ numNum++; } else if(code >= 65 && code <= 90){ upperNum++; } else if(code >= 97 && code <= 122){ lowerNum++; } else{ return false; } } return lowerNum && upperNum && numNum; };
Finally, the length detection and content detection are integrated to form the password verification function:
function validate(password) { return lengthValid(password) && contentValid(password); }
The above is the content of JavaScript interesting question: password verification. For more related content, please pay attention to PHP Chinese Net (www.php.cn)!