Instantly share code, notes, and snippets.

Embed
What would you like to do?
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