Skip to content

Instantly share code, notes, and snippets.

@dacur
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save dacur/d264e04312d087cb1237 to your computer and use it in GitHub Desktop.

Select an option

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.).
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