JavaScript - Validate Password in JS Last Updated : 16 Jan, 2025 Comments Improve Suggest changes Like Article Like Report 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 OutputGeeks@123 true GeeksforGeeks false Geeks123 false Regula Expression Breakdown:^(?=.*[a-z]): Ensures at least one lowercase letter.(?=.*[A-Z]): Ensures at least one uppercase letter.(?=.*\d): Ensures at least one digit.(?=.*[@$!%*?&]): Ensures at least one special character.[A-Za-z\d@$!%*?&]{8,}$: Ensures the password is at least 8 characters long.Multiple Output based on Password StrengthIn 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 Outputu4thdkslfheogica - Too lengthy G!2ks - Too short GeeksforGeeks - Weak Geeks123 - Medium GEEKS123 - Weak Geeks@123# - Strong Comment More infoAdvertise with us Next Article JavaScript - Validate Password in JS J jatinsharmatu54 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-RegExp JavaScript-Questions JavaScript-Program +1 More Similar Reads Validate a password using HTML and JavaScript Validating a password using HTML and JavaScript involves ensuring that user-entered passwords meet certain criteria, such as length, complexity, or character types (e.g., uppercase, lowercase, numbers, and symbols). This process enhances security by enforcing strong password requirements before form 2 min read Password Validation Form Using JavaScript The password Validation form is used to check the password requirements such as the password must have at least one Uppercase, or lowercase, number, and the length of the password. We can also check this after submitting the form but it's not recommended. We can easily check before submitting the fo 4 min read How to validate confirm password using JavaScript ? In almost every modern website we have seen a login and signup feature, which is surely an important service for both the client and the service provider. When a user signs up for a certain website they have to put in a username a password, to enter the password websites ask them to enter a password 3 min read JavaScript regex - Validate Credit Card in JS To validate a credit card number in JavaScript we will use regular expression combined with Luhn's algorithm. Appling Luhn's algorithm to perform a checksum validation for added securityLuhn algorithm:It first sanitizes the input by removing any non-digit characters (e.g., spaces).It then processes 3 min read Password Matching using JavaScript In an online application form or social media signup page, it's common to have two input fields: one for the password and another for confirming the password. The goal is to check whether the entered passwords match.To achieve this, the first password is stored in a variable password1, and the confi 2 min read Like