Oops! It appears that you have disabled your Javascript. In order for you to see this page as it is meant to appear, we ask that you please re-enable your Javascript!

Confirm password with form validation in JavaScript Code examples

Confirm password with form validation in JavaScript Code examples

Form validation is a process to validate that user is submitting the data according to correct format or not. Most of the time it happens that user enter the password during registration and he thinks that he enter the password as he thinks but unfortunately due to the wrong fingers on the keyboard his password is set to some other password.

For example, the user wants to enter the password as;

Password: asdf

but mistakenly he enters the password as follows; 

Password: asdg

In such conditions, there is a problem for the user. To avoid this problem, we as a web developer asks the user to enter the password two times.  Form validation can be done by JavaScript.

Example of code for Confirm password form validation in JavaScript

<html>
<head>
 <script type="text/javascript">
        function Validate() {
            var password = document.getElementById("1stPassword").value;
            var confirmPassword = document.getElementById("ConfirmPassword").value;
            if (password != confirmPassword) {
                alert("You first Passwords is not similar with 2nd password. Please enter same password in both");
                return false;
            }
            return true;
        }
    </script>
	</head>
<body>
 Enter 1st Password:
<input type="password" id="1stPassword" />
Enter 2nd password to Confirm 1st Password:
<input type="password" id="ConfirmPassword" />
<input type="button" id="btnSubmit" value="Submit" onclick="return Validate()" />
          
</body>
</html>

 

Example of code for Confirm password form validation in JavaScript with detailed HTML code

<html>
<head>
 <script type="text/javascript">
        function Validate() {
            var password = document.getElementById("1stPassword").value;
            var confirmPassword = document.getElementById("ConfirmPassword").value;
            if (password != confirmPassword) {
                alert("You first Passwords is not similar with 2nd password. Please enter same password in both");
                return false;
            }
            return true;
        }
    </script>
	</head>
<body>
    <table border="1" cellspacing="2" cellpadding="5" >
        <tr>
            <td>
                Enter 1st Password:
            </td>
            <td>
                <input type="password" id="1stPassword" />
            </td>
        </tr>
        <tr>
            <td>
               Enter 2nd password to Confirm 1st Password:
            </td>
            <td>
                <input type="password" id="ConfirmPassword" />
            </td>
        </tr>
        <tr>
            <td>
            </td>
            <td>
                <input type="button" id="btnSubmit" value="Submit" onclick="return Validate()" />
            </td>
        </tr>
    </table>
  
</body>
</html>

Topic Covered

Confirm password with form validation in JavaScript Code examples

  List of All Programs of Javascript : Click Here 
.

Prof. Fzal Rehman Shamil
Instructor, Researcher, Blogger, SEO Expert, Poet and Publisher of International Journal Of Software, Technology & Science ISSN : 2616 - 5325