Confirm password validation in JavaScript

Confirm password validation in JavaScript

In this section, we will examine password validation utilizing JavaScript. We have to approve a password each time at whatever point a client makes a record on any site or application. In this way, we need to check a legitimate password just as put the Confirm password validation.

For a substantial password, the accompanying boundaries must be contained by it to be legitimate -

A password ought to be alphanumeric.

First letter of the password ought to be capital.

Password must contain a unique character (@, $, !, and, and so forth)

Password length must be more noteworthy than 8 characters.

One of the most significant that the password fields ought not be unfilled.

At whatever point a client makes a password, there is consistently one more field of affirm password. It watches that the password entered by the client is same as this affirm password fields. To make a legitimate password, both the password and affirm password fields esteem must be coordinated.

Initial one, we will check for a legitimate password and afterward affirm password validation checks.

f:id:javatpoint:20201024235743j:plain

Confirm password validation in JavaScript

Legitimate password Validation

In this model, we will watch that the password made by the client is legitimate or not and coordinate with all the boundary examined previously. See the code beneath for password check.

 

Example:

  1. <html>  
  2. <head>  
  3. <title> Verification of valid Password </title>  
  4. </head>  
  5. <script>  
  6. function verifyPassword() {  
  7.   var pw = document.getElementById("pswd").value;  
  8.   //check empty password field  
  9.   if(pw == "") {  
  10.      document.getElementById("message").innerHTML = "**Fill the password please!";  
  11.      return false;  
  12.   }  
  13.    
  14.  //minimum password length validation  
  15.   if(pw.length < 8) {  
  16.      document.getElementById("message").innerHTML = "**Password length must be atleast 8 characters";  
  17.      return false;  
  18.   }  
  19.   
  20. //maximum length of password validation  
  21.   if(pw.length > 15) {  
  22.      document.getElementById("message").innerHTML = "**Password length must not exceed 15 characters";  
  23.      return false;  
  24.   } else {  
  25.      alert("Password is correct");  
  26.   }  
  27. }  
  28. </script>  
  29.   
  30. <body>  
  31. <center>  
  32. <h1 style="color:green">Javatpoint</h1>  
  33. <h3> Verify valid password Example </h3>  
  34.   
  35. <form onsubmit ="return verifyPassword()">  
  36. <!-- Enter Password -->  
  37. <td> Enter Password </td>  
  38. <input type = "password" id = "pswd" value = "">   
  39. <span id = "message" style="color:red"> </span> <br><br>  
  40.   
  41. <!-- Click to verify valid password -->  
  42. <input type = "submit" value = "Submit">  
  43.   
  44. <!-- Click to reset fields -->  
  45. <button type = "reset" value = "Reset" >Reset</button>  
  46. </form>  
  47. </center>  
  48. </body>  
  49. </html>  

 

Run This Code