Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Program to Validate Password using Regular Expressions

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}$/;

Approaches to Validate Passwords using Regex

  • Simple boolean output
  • Multiple outcomes based on password strength

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 = "neveropen";
let pass3 = "Geeks123";
  
console.log(pass1, regex.test(pass1));
console.log(pass2, regex.test(pass2));
console.log(pass3, regex.test(pass3));


Output

Geeks@123 true
neveropen false
Geeks123 false

Multiple Output based on Password Strength

In this method, we will set the password strength on the basis of the number of combinations for numbers, letters, special symbols, etc.

Example: This example will validate the password on the basis of password strength.

Javascript




const strength = {
    1: "very Weak",
    2: "Weak",
    3: "Meduim",
    4: "Strong",
};
function checkStrength(pass) {
    if (pass.length > 15)
        return console.log(
            pass + " Password is too lengthy"
        );
    else if (pass.length < 8)
        return console.log(
            pass + " Password is too short"
        );
  
    let regex =
/^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@.#$!%*?&])[A-Za-z\d@.#$!^%*?&]{8,15}$/;
    if (regex.test(pass)) {
        return console.log(pass 
        + " Password is strong");
    }
    let count = 0;
    let regex1 = /[a-z]/;
    if (regex1.test(pass)) count++;
    let regex2 = /[A-Z]/;
    if (regex2.test(pass)) count++;
    let regex3 = /[\d]/;
    if (regex3.test(pass)) count++;
    let regex4 = /[!@#$%^&*.?]/;
    if (regex4.test(pass)) count++;
  
    console.log(pass, "Pasword is " + strength[count]);
}
let passwords = [
    "u4thdkslfheogica",
    "G!2ks",
    "neveropen",
    "Geeks123",
    "GEEKS123",
    "Geeks@123#",
];
passwords.map((e) => checkStrength(e));


Output

u4thdkslfheogica Password is too lengthy
G!2ks Password is too short
neveropen Pasword is Weak
Geeks123 Pasword is Meduim
GEEKS123 Pasword is Weak
Geeks@123# Password is strong
Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments