let pattern = new RegExp("^(?=(.*[a-zA-Z]){1,})(?=(.*[0-9]){2,}).{8,}$"); //Regex: At least 8 characters with at least 2 numericals
let inputToListen = document.getElementById('pass-one'); // Get Input where psw is write
let valide = document.getElementsByClassName('indicator')[0]; //little indicator of validity of psw
inputToListen.addEventListener('input', function () { // Add event listener on input
if(pattern.test(inputToListen.value)){
valide.innerHTML = 'ok';
}else{
valide.innerHTML = 'not ok'
}
});
// Commonly used regular expression
// Check 2-9 characters, false if not matched, true if matched
const validateName = (name) => {
const reg = /^[u4e00-u9fa5]{2,9}$/;
return reg.test(name);
};
// Verify phone number
const validatePhoneNum = (mobile) => {
const reg = /^1[3,4,5,6,7,8,9]d{9}$/;
return reg.test(mobile);
};
// Check passwords consisting of 6 to 18 uppercase and lowercase alphanumeric underscores
const validatePassword = (password) => {
const reg = /^[a-zA-Z0-9_]{6,18}$/;
return reg.test(password);
};
/^ : 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,}$/
// Commonly used regular expression
// Check 2-9 characters, false if not matched, true if matched
const validateName = (name) => {
const reg = /^[u4e00-u9fa5]{2,9}$/;
return reg.test(name);
};
// Verify phone number
const validatePhoneNum = (mobile) => {
const reg = /^1[3,4,5,6,7,8,9]d{9}$/;
return reg.test(mobile);
};
// Check passwords consisting of 0 to 8
//uppercase and lowercase
//alphanumeric underscores
const validatePassword = (password) => {
const reg = /^[a-zA-Z0-9_]{0,9}$/;
return reg.test(password);
};