Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • JS Tutorial
  • JS Exercise
  • JS Interview Questions
  • JS Array
  • JS String
  • JS Object
  • JS Operator
  • JS Date
  • JS Error
  • JS Projects
  • JS Set
  • JS Map
  • JS RegExp
  • JS Math
  • JS Number
  • JS Boolean
  • JS Examples
  • JS Free JS Course
  • JS A to Z Guide
  • JS Formatter
Open In App
Next Article:
How to Check if a String Contains a Valid URL Format in JavaScript ?
Next article icon

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 } 

Output
Geeks@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 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 

Next Article
How to Check if a String Contains a Valid URL Format in JavaScript ?

J

jatinsharmatu54
Improve
Article Tags :
  • JavaScript
  • Web Technologies
  • JavaScript-RegExp
  • JavaScript-Questions
  • JavaScript-Program

Similar Reads

  • JavaScript - Validate Password in JS
    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. [GFGTABS] JavaScript let regex = /^(?=.*[a-z])(?=.*[A-Z])(?=.*
    2 min read
  • How to Check if a String Contains a Valid URL Format in JavaScript ?
    A string containing a valid URL format adheres to standard conventions, comprising a scheme (e.g., "http://" or "https://"), domain, and optionally, a path, query parameters, and fragments. Ensuring this format is crucial for data consistency and accurate handling of URLs in applications. There are
    2 min read
  • JavaScript Program to Validate String for Uppercase, Lowercase, Special Characters, and Numbers
    In this article, we are going to learn how can we check if a string contains uppercase, lowercase, special characters, and numeric values. We have given string str of length N, the task is to check whether the given string contains uppercase alphabets, lowercase alphabets, special characters, and nu
    4 min read
  • How to Check Case of Letter in JavaScript ?
    There are different ways to check the case of the letters in JavaScript which are explained below comprehensively, one can choose the method that best suits their specific use case and requirement. Table of Content Using the function "toUpperCase()" or "toLowerCase"Using regular expressionUsing ASCI
    3 min read
  • JavaScript Program to Match Single Character with Multiple Possibilities
    In this article, we will write programs to check all possible matching in JavaScript. We will check the occurrence of characters individually in the given string or sequence. In this article, we will check the occurrence of vowels in the given string. Table of Content Using JavaScript RegExp test()
    3 min read
  • JavaScript - String Contains Only Alphabetic Characters or Not
    Here are several methods to check if a string contains only alphabetic characters in JavaScript Using Regular Expression (/^[A-Za-z]+$/) - Most USedThe most common approach is to use a regular expression to match only alphabetic characters (both uppercase and lowercase). [GFGTABS] JavaScript let s =
    2 min read
  • JavaScript - Check if a String Contains Any Digit Characters
    Here are the various methods to check if a string contains any digital character 1. Using Regular Expressions (RegExp)The most efficient and popular way to check if a string contains digits is by using a regular expression. The pattern \d matches any digit (0-9), and with the test() method, we can e
    4 min read
  • JavaScript - Validate URL in JS
    We will explore different approaches to validating URLs in JavaScript. These approaches include using regular expressions (regex), the URL constructor, and various npm packages. let's see one by one. All valid URLs follow a particular pattern. They have three main parts, which are: ProtocolDomain na
    2 min read
  • 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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences