var strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#$%^&*])(?=.{8,})");
RegEx Description
^ The password string will start this way
(?=.*[a-z]) The string must contain at least 1 lowercase alphabetical character
(?=.*[A-Z]) The string must contain at least 1 uppercase alphabetical character
(?=.*[0-9]) The string must contain at least 1 numeric character
(?=.*[!@#$%^&*]) The string must contain at least one special character, but we are escaping reserved RegEx characters to avoid conflict
(?=.{8,}) The string must be eight characters or longer
by- Nic Raboy
/^ : Start
(?=.{8,}) : Length
(?=.*[a-zA-Z]) : Letters
(?=.*d) : Digits
/^(?=.*[!#$%&?*^()~` "])$/ : Special characters
$/ : End
(/^
(?=.*d) //should contain at least one digit
(?=.*[a-z]) //should contain at least one lower case
(?=.*[A-Z]) //should contain at least one upper case
[a-zA-Z0-9]{8,} //should contain at least 8 from the mentioned characters
$/)
Example:- /^(?=.*d)(?=.*[a-zA-Z])[a-zA-Z0-9]{7,}$/
^ Start anchor
(?=.*[A-Z].*[A-Z]) Ensure string has two uppercase letters.
(?=.*[!@#$&*]) Ensure string has one special case letter.
(?=.*[0-9].*[0-9]) Ensure string has two digits.
(?=.*[a-z].*[a-z].*[a-z]) Ensure string has three lowercase letters.
.{8} Ensure string is of length 8.
$ End anchor.
function validate() {
var p = document.getElementById('pass').value
var errors = []
//if (p.length < 8) {
// errors.push("Your password must be at least 8 characters")
//}
if (p.search(/[a-z]/) < 0) {
errors.push("Your password must contain at least one lowercase letter.")
}
if (p.search(/[A-Z]/) < 0) {
errors.push("Your password must contain at least one uppercase letter.")
}
if (p.search(/[0-9]/) < 0) {
errors.push("Your password must contain at least one digit.")
}
if(p.search(/[!@#$\%^&*()\_+.,;:-]/) < 0) {
errors.push("Your password must contain at least one special character.")
}
if (errors.length > 0) {
document.getElementById("errors").innerHTML = errors.join("<br>")
return false;
}
return true;
}