Simple Boolean Output
Example: In this example, we will only validate if the given password is strong or not and output true or false.
Javascript
let regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@. #$!%*?&])[A-Za-z\d@.#$!%*?&]{8,15}$/; let pass1 = "Geeks@123" ; let pass2 = "w3wiki" ; let pass3 = "Geeks123" ; console.log(pass1, regex.test(pass1)); console.log(pass2, regex.test(pass2)); console.log(pass3, regex.test(pass3)); |
Geeks@123 true w3wiki false Geeks123 false
JavaScript Program to Validate Password using Regular Expressions
In this article, we will see the JavaScriopt program to check and validate a strong or weak password with a custom-defined range. To accomplish this task we will use JavaScript Regular Expression.
A Regular Expression is a sequence of characters that forms a search pattern. The search pattern can be used for text search and text to replace operations. A regular expression can be a single character or a more complicated pattern.
A strong password will be of a minimum 8 characters to max range according to user need (max limit is set to 15 characters for this post). It must include the following:
- At least one lowercase alphabet i.e. [a-z]
- At least one uppercase alphabet i.e. [A-Z]
- At least one Numeric digit i.e. [0-9]
- At least one special character i.e. [‘@’, ‘$’, ‘.’, ‘#’, ‘!’, ‘%’, ‘*’, ‘?’, ‘&’, ‘^’]
- Also, the total length must be in the range [8-15]
Syntax:
The required regex for a String password will be:
let regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@.#$!%*?&^])[A-Za-z\d@.#$!%*?&]{8,15}$/;