Confirm password validation in JavaScript
In this tutorial, we are going to see how to validate the password in Javascript. A password is correct if it contains:
- At least 1 uppercase character.
- At least 1 lowercase character.
- At least 1 digit.
- At least 1 special character.
- Minimum 10 characters.
Script to validate the password in Javascript / HTML
<!DOCTYPE html> <html> <head> <script type="text/javascript"> function validate() { var msg; var str = document.getElementById("password").value; if (str.match( /[0-9]/g) && str.match( /[A-Z]/g) && str.match(/[a-z]/g) && str.match( /[^a-zA-Z\d]/g) && str.length >= 10) msg = "<p style='color:green'>Strong password.</p>"; else msg = "<p style='color:red'>Weak password.</p>"; document.getElementById("msg").innerHTML= msg; } </script> </head> <body> Enter the password: <input id="password" /> <button onclick="validate()">Validate</button><br> <span id="msg"></span> </body> </html>
Result |
---|
Enter the password:
|