Password validation
Sometimes a password validation in a form is essential. You can create a password in different ways, it's structure may be simple, reasonable or strong. Here we validate various type of password structure through JavaScript codes and regular expression.
- Check a password between 7 to 16 characters which contain only characters, numeric digits and underscore and first character must be a letter.
- Check a password between 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter.
- Check a password between 7 to 15 characters which contain at least one numeric digit and a special character.
- Check a password between 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character.
Following code blocks contain actual codes for the said validations. We have kept the CSS code part common for all the validations.
CSS Code:
To check a password between 7 to 16 characters which contain only characters, numeric digits, underscore and first character must be a letter
To validate the said format we use the regular expression ^[A-Za-z]\w{7,15}$, where \w matches any word character (alphanumeric) including the underscore (equivalent to [A-Za-z0-9_]). Next the match() method of string object is used to match the said regular expression against the input value. Here is the complete web document.
HTML Code:
JavaScript Code:
View the example in the browser
To check a password between 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter
To validate the said format we use the regular expression ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{4,8}$. Next the match() method of string object is used to match the said regular expression against the input value. Here is the complete web document.
HTML Code:
JavaScript Code:
View the example in the browser
To check a password between 7 to 15 characters which contain at least one numeric digit and a special character
To validate the said format we use the regular expression ^^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$. Next the match() method of string object is used to match the said regular expression against the input value. Here is the complete web document.
HTML Code:
JavaScript Code
View the example in the browser
To check a password between 8 to 15 characters which contain at least one lowercase letter, one uppercase letter, one numeric digit, and one special character
To validate the said format we use the regular expression ^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$. Next the match() method of string object is used to match the said regular expression against the input value. Here is the complete web document.
HTML Code:
JavaScript Code:
View the example in the browser
Source & Credit: https://www.w3resource.com/javascript/form/password-validation.php
No comments:
Post a Comment
Please Comment Here!