html password validation code example
Example 1: javascript password matching
let pass1 = document.getElementById("pass-one");
let pass2 = document.getElementById("pass-two");
let run = document.getElementById("run");
run.addEventListener("click", function () {
if (pass1.value !== pass2.value) {
pass1.style.border = '3px solid red';
pass2.style.border = '3px solid red';
run.innerHTML = "Mismatch";
} else {
pass1.style.border = '3px solid green';
pass2.style.border = '3px solid green';
run.innerHTML = "Matching"
}
})
Example 2: Javascript validate password
let pattern = new RegExp("^(?=(.*[a-zA-Z]){1,})(?=(.*[0-9]){2,}).{8,}$");
let inputToListen = document.getElementById('pass-one');
let valide = document.getElementsByClassName('indicator')[0];
inputToListen.addEventListener('input', function () {
if(pattern.test(inputToListen.value)){
valide.innerHTML = 'ok';
}else{
valide.innerHTML = 'not ok'
}
});
Example 3: password validation with regular expression in javascript
var strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");
Example 4: html add password
<input type="password" name="password">