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 info J jatinsharmatu54 Follow Improve Article Tags : JavaScript Web Technologies JavaScript-RegExp JavaScript-Questions JavaScript-Program +1 More Explore JavaScript BasicsIntroduction to JavaScript4 min readVariables and Datatypes in JavaScript6 min readJavaScript Operators5 min readControl Statements in JavaScript4 min readArray & StringJavaScript Arrays7 min readJavaScript Array Methods7 min readJavaScript Strings6 min readJavaScript String Methods9 min readFunction & ObjectFunctions in JavaScript5 min readJavaScript Function Expression3 min readFunction Overloading in JavaScript4 min readObjects in Javascript4 min readJavaScript Object Constructors4 min readOOPObject Oriented Programming in JavaScript3 min readClasses and Objects in JavaScript4 min readWhat Are Access Modifiers In JavaScript ?5 min readJavaScript Constructor Method7 min readAsynchronous JavaScriptAsynchronous JavaScript2 min readJavaScript Callbacks4 min readJavaScript Promise4 min readEvent Loop in JavaScript4 min readAsync and Await in JavaScript2 min readException HandlingJavascript Error and Exceptional Handling6 min readJavaScript Errors Throw and Try to Catch2 min readHow to create custom errors in JavaScript ?2 min readJavaScript TypeError - Invalid Array.prototype.sort argument1 min readDOMHTML DOM (Document Object Model)9 min readHow to select DOM Elements in JavaScript ?3 min readJavaScript Custom Events4 min readJavaScript addEventListener() with Examples9 min readAdvanced TopicsClosure in JavaScript4 min readJavaScript Hoisting6 min readJavascript Scope3 min readJavaScript Higher Order Functions7 min readDebugging in JavaScript4 min read Like