Skip to content

Instantly share code, notes, and snippets.

@musicm122
Last active August 29, 2015 13:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save musicm122/9635212 to your computer and use it in GitHub Desktop.
Save musicm122/9635212 to your computer and use it in GitHub Desktop.
Javascript Validation regex
var hasLowercase = function (val) {
var patt = new RegExp(/([a-z]+)/g);//checks for 1 or more a-z characters.
return patt.test(val);
};
var hasUppercase = function (val) {
var patt = new RegExp(/([A-Z]+)/g);//checks for 1 or more A-Z characters.
return patt.test(val);
};
var hasDigit = function (val) {
var patt = new RegExp(/([d]+)/g);//checks for 1 or more digit character.
return patt.test(val);
};
var hasSpecialChar = function (val) {
//checks for 1 or more special character. this includes the following ~`!@#$%^&*()_-+={}[]|\<>,./?;':"
var patt = new RegExp(/([\~\`\!\@\#\$\%\^\&\*\(\)\_\+\-\=\|\}\{\\\]\[\'\;\"\:\/\.\,\?\>;\<;])+/g);
return patt.test(val);
};
//using the previous set of functions this one checks for 1 uppercase, 1 lowercase, 1 digit and 1 special character
var IsValidExpression = function (testVal) {
var retval = hasLowercase(testVal) && hasUppercase(testVal) && hasDigit(testVal) && hasSpecialChar(testVal);
return retval;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment