How To Use Password Validation Example Using JavaScript?

Nov 12, 2021 . Admin



Hello Friends,

Now let's see example of how to use password validation example. We will use password validation in javascript. Here you will learn how to use password validation. Let's get started with how to use password validation in javascript.

Here i will give you many example how to use password validation javascript.

Example : 1
<html>
    <head>
        <title>How To Use Password Validation Example Using JavaScript - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Use Password Validation Example Using JavaScript - MyWebtuts.com</h3>

        <form onsubmit ="return verifyPassword()">
            <td> Enter Password :</td>
            <input type = "password" id = "passwd" value = ""> 
            <span id ="message" style="color:red"></span> <br><br>

            <input type ="submit" value ="Submit">

            <button type ="reset" value ="Reset">Reset</button>
        </form>

        <script>
            function verifyPassword() {
                var a1 = document.getElementById("passwd").value;

                if(a1 == "") {
                    document.getElementById("message").innerHTML = "*Fill the password please!";
                    return false;
                }

                if(a1.length < 8) {
                    document.getElementById("message").innerHTML = "*Password length must be atleast 8 characters";
                    return false;
                }

                if(a1.length > 15) {
                    document.getElementById("message").innerHTML = "*Password length must not exceed 15 characters";
                    return false;
                } else {
                    alert("Password is correct");
                }
            }
        </script>
    </body>
</html>
Output :
Enter Password : 123456
*Password length must be atleast 8 characters
Example : 2
<html>
    <head>
        <title>How To Use Password Validation Example Using JavaScript - MyWebtuts.com</title>
    </head>
    <body>
        <h3>How To Use Password Validation Example Using JavaScript - MyWebtuts.com</h3>

        <form onsubmit ="return validateFrm()">
            <td> Create Password *: </td>
            <input type = "password" id = "a1" value = ""> 
            <span id = "msg1" style="color:red"> </span> <br><br>

            <td> Confirm Password *: </td>
            <input type = "password" id = "a2" value = ""> 
            <span id = "msg2" style="color:red"> </span> <br><br>

            <input type = "submit" value = "Submit">
            <button type = "reset" value = "Reset" >Reset</button>
        </form>
        <script>
            function validateFrm() {
                var pwd1 = document.getElementById("a1").value;
                var pwd2 = document.getElementById("a2").value;
              
                if(pwd1 == "") {
                document.getElementById("msg1").innerHTML = "*Fill the password please!";
                return false;
                }

                if(pwd2 == "") {
                document.getElementById("msg2").innerHTML = "*Enter the password please!";
                return false;
                } 

                if(pwd1.length < 8) {
                    document.getElementById("msg1").innerHTML = "*Password length must be atleast 8 characters";
                    return false;
                }

                if(pwd1.length > 15) {
                    document.getElementById("msg1").innerHTML = "*Password length must not exceed 15 characters";
                    return false;
                }

                if(pwd1 != pwd2) {
                    document.getElementById("msg2").innerHTML = "*Passwords are not same";
                    return false;
                } else {
                    alert ("Your password created successfully");
                        document.write("JavaScript Form Has Been Submitted Successfully");
                }
            }
        </script>
    </body>
</html>
Output :
Create Password *: 1234567 (*Password length must be atleast 8 characters)
Confirm Password *: 1234568 (*Passwords are not same)

It will help you...

#Javascript