JS Input Validation – IV (Password)
Sometimes a password validation in a HTML form is essential. We can create a password in different ways; its structure may be simple, reasonable or strong. Here we validate various type of password structure through JavaScript codes and regular expression.
The following scenarios may be considered—
1. Check a password between 7 to 16 characters which contain only characters, numeric digits and underscore and first character must be a letter.
2. Check a password between 6 to 20 characters which contain at least one numeric digit, one uppercase and one lowercase letter.
3. Check a password between 7 to 15 characters which contain at least one numeric digit and a special character.
4. 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.
Explanation
The regular expression patterns are given one-by-one below:
1. var passwd = /^[A-Za-z]\w{7,14}$/;
2. var passwd = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z]).{6,20}$/;
3. var passwd = /^(?=.*[0-9])(?=.*[!@#$%^&*])[a-zA-Z0-9!@#$%^&*]{7,15}$/;
4. var passwd = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[^a-zA-Z0-9])(?!.*\s).{8,15}$/;
It is now up to the readers to write the complete code for password validation.