password and confirm password validation js 1

password and confirm password validation js

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

Here is what the above code is Doing:
1. We create a regular expression that will check if the password is valid.
2. We get the input where the password is written.
3. We get the little indicator of validity of the password.
4. We add an event listener on the input.
5. We check if the password is valid.
6. If it is, we change the indicator to “ok”.
7. If it is not, we change the indicator to “not ok”.

Now, let’s add the code to check if the password is the same in both inputs.

Similar Posts