A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript-program-to-validate-password-using-regular-expressions/ below:

JavaScript - Validate Password in JS

JavaScript - Validate Password in JS

Last Updated : 16 Jan, 2025

To validate a password in JavaScript regex to ensure it meets common requirements like length, uppercase letters, lowercase letters, numbers, and special characters. we will validate a strong or weak password with a custom-defined range.

JavaScript
let regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@.#$!%*?&])[A-Za-z\d@.#$!%*?&]{8,15}$/;
let s1 = "Geeks@123";
let s2 = "GeeksforGeeks";
let s3 = "Geeks123";
console.log(s1, regex.test(s1));
console.log(s2, regex.test(s2));
//Driver Code Starts
console.log(s3, regex.test(s3));
//Driver Code Ends

Output
Geeks@123 true
GeeksforGeeks false
Geeks123 false
Regula Expression Breakdown: 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.

JavaScript
//Driver Code Starts
const levels = {
    1: "Very Weak",
    2: "Weak",
    3: "Medium",
    4: "Strong",
};

//Driver Code Ends
function checkPwd(pwd) {
    if (pwd.length > 15) {
        return console.log(pwd + " - Too lengthy");
    } else if (pwd.length < 8) {
        return console.log(pwd + " - Too short");
    }

    const checks = [
        /[a-z]/,     // Lowercase
        /[A-Z]/,     // Uppercase
        /\d/,        // Digit
        /[@.#$!%^&*.?]/ // Special character
    ];
    let score = checks.reduce((acc, rgx) => acc + rgx.test(pwd), 0);

    console.log(pwd + " - " + levels[score]);
}
//Driver Code Starts

let pwds = [
    "u4thdkslfheogica",
    "G!2ks",
    "GeeksforGeeks",
    "Geeks123",
    "GEEKS123",
    "Geeks@123#",
];

pwds.forEach(checkPwd);
//Driver Code Ends

Output
u4thdkslfheogica - Too lengthy
G!2ks - Too short
GeeksforGeeks - Weak
Geeks123 - Medium
GEEKS123 - Weak
Geeks@123# - Strong


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4