Password validation using JavaScript Examples

Example 1: javascript password validation special character

function CheckPassword(inputtxt) 
{ 
var paswd=  /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$/;
if(inputtxt.value.match(paswd)) 
{ 
alert('Correct, try another...')
return true;
}
else
{ 
alert('Wrong...!')
return false;
}
}

Example 2: 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 3: password and confirm password validation js

var check = function() {
  if (document.getElementById('password').value ==
    document.getElementById('confirm_password').value) {
    document.getElementById('message').style.color = 'green';
    document.getElementById('message').innerHTML = 'matching';
  } else {
    document.getElementById('message').style.color = 'red';
    document.getElementById('message').innerHTML = 'not matching';
  }
}

Example 4: Javascript validate password

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'
        }
    });

Example 5: password validation with regular expression in javascript

var strongRegex = new RegExp("^(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*[!@#\$%\^&\*])(?=.{8,})");