• Register
499 points
‚óŹ3

How to match password and confirm the password in javascript
 

Password validation is a process in which we have to validate the password that is strong and acquiring all the points that we want, like having a character more than 10 or containing any special character. In this javascript lecture, we are going to discuss password validation using JavaScript. Every time user creates an account we need to verify the password is passing all the basic requirements for the password. To make it more clear we can use another field that confirms the password. For a valid password, the following parameters must be contained by it to be valid -

  • The password should be alphanumeric.
  • The first letter of the password must be capital or number.
  • Password must contain a special character (@, $, ! &, etc) or doesn’t contain at all.
  • Password length must be greater than 8 characters or more than 10.
  • One of the most important that the password fields should not be empty, to create unnecessary spam users.

when a user creates an account normally web app confirms over there by the user that you have entered the right password by re-entering the password again by the user and make it more clear.

The first web app checks for the valid password after it, it will check for the reconfirm password.

Valid password Validation

In this example, we will check that the password created by the user is valid or not and match with all the parameters discussed above. Password verification Example.Example based on the password validation from the user side.

<html>  
<head>  
<title> Verification of valid Password </title>  
</head>  
<script>  
function verifyPassword() {  
  var pw = document.getElementById("pswd").value;  
  //check empty password field  
  if(pw == "") {  
     document.getElementById("message").innerHTML = "**Fill the password please!";  
     return false;  
  }  
   
 //minimum password length validation  
  if(pw.length < 8) {  
     document.getElementById("message").innerHTML = "**Password length must be atleast 8 characters";  
     return false;  
  }  
  
//maximum length of password validation  
  if(pw.length > 15) {  
     document.getElementById("message").innerHTML = "**Password length must not exceed 15 characters";  
     return false;  
  } else {  
     alert("Password is correct");  
  }  
}  
</script>  
  
<body>  
<center>  
<h1 style="color:green">Javatpoint</h1>  
<h3> Verify valid password Example </h3>  
  
<form onsubmit ="return verifyPassword()">  
<!-- Enter Password -->  
<td> Enter Password </td>  
<input type = "password" id = "pswd" value = "">   
<span id = "message" style="color:red"> </span> <br><br>  
  
<!-- Click to verify valid password -->  
<input type = "submit" value = "Submit">  
  
<!-- Click to reset fields -->  
<button type = "reset" value = "Reset" >Reset</button>  
</form>  
</center>  
</body>  
</html>