Last active
August 29, 2015 14:13
-
-
Save dacur/d264e04312d087cb1237 to your computer and use it in GitHub Desktop.
Password validation using regular expressions. Returns an error message based on what is missing/wrong with the user's password (or returns "ok"). Password must contain at least one letter and one number, must be between 6 and 50 characters in length, and may contain certain special characters (such as !, #, &, etc.).
This file contains hidden or 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
| function ValidatePassword(str){ | |
| if (str.length < 6) { | |
| return("Password is too short. Must be at least 6 characters."); | |
| } else if (str.length > 50) { | |
| return("Password is too long. Must be no more than 50 characers."); | |
| } else if (str.search(/\d/) == -1) { | |
| return("Password must contain at least one number."); | |
| } else if (str.search(/[a-zA-Z]/) == -1) { | |
| return("Password must contain at least one letter."); | |
| } else if (str.search(/[^a-zA-Z0-9\!\@\#\$\%\^\&\*\(\)\_\+]/) != -1) { | |
| return("Password contains a character that is not allowed. Please try again."); | |
| } | |
| return("ok"); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment