Skip to content

Instantly share code, notes, and snippets.

@rogers0404
Last active March 31, 2024 21:37
Show Gist options
  • Save rogers0404/36762d5e2e60fc6bedc3a2a33c7b1310 to your computer and use it in GitHub Desktop.
Save rogers0404/36762d5e2e60fc6bedc3a2a33c7b1310 to your computer and use it in GitHub Desktop.
Regular Expressions (Regex) for Password Validation

Regular Expressions (Regex) for Password Validation

Sometime you need some help to search or validate entries in your code that matches certain of pattern, It can cause so much deception because the amount of code to accomplish the task. That is Regex (Regular Expressions) comes to help us to minimize our job.

Regex is a sequence of characters that define a search pattern. Such patterns are used by searching for one or more that matches of a specific string or for input validation

Summary

It will present a Password Validation with Regular Expressions (Regex)

Regex: ^(?=.*\d)(?=.*[a-zA-Z])(?=.*[A-Z])(?=.*[-\#\$\.\%\&\*])(?=.*[a-zA-Z]).{8,16}$

The password must match:

  • At least 8 - 16 characters,
  • must contain at least 1 uppercase letter,
  • must contain at least 1 lowercase letter,
  • and 1 number
  • Can contain any of this special characters $ % # * & - .

It is written in 5 Look-ahead groups that matches what is looking for:

  • First part: ^ ... $ The beginning and End expressions,

  • Second part: ^ ... .{8,16}$ the Dot and the Quantifier {8, 16}. That describes any of the preceding (that matches the password requirements ) characters limited with 8 to 16 characters long.

  • Third part: (?=.*\d)(?=.*[a-zA-Z])(?=.*[A-Z])(?=.*[-\#\$\.\%\&\*])(?=.*[a-zA-Z]). It has 5 groups

    • First group: (?=.*\d). Look-ahead that matches the a set of any characters (.*) without show them to the result and following of digits.

    • Second, Third and Fifth group: (?=.*[a-zA-Z])(?=.*[A-Z]) ... (?=.*[a-zA-Z]). Look-ahead that matches the a set of any characters (.*) without show them to the result and following of any uppercase and lowercase letters.

    • Fourth Group: (?=.*[-\#\$\.\%\&\*]). Look-ahead that matches the a set of any characters (.*) without show them to the result and following of a special characters - # $ . % & *. Notice each special character has before them a Class called backslash \ except the dash symbol -

Note: the ... sequential points are examples to explain the step by step the Regex

Table of Contents

Regex Components

Anchors

^ : Beginning, Matches the beginning of the string
$ : End, Matches the end of the string

Quantifiers

* : Star, Matches 0 or more of the preceding characters
+ : Plus, Matches 1 or more of the preceding characters
{8, 16} : Specific Quantifier, Matches from 8 to 16 of the preceding characters

Character Classes

.  : Dot, Matches any character except linebreaks
[] : Characters set / OR Operator / Bracket Expressions, Match any character in the set
\d : Digit, Matches any digit character (0-9)
\  : backslash, operator to capture special characters such as $ % # * & - .]

Grouping and Capturing

() : Capturing Group, Groups multiple tokens together and creates a capture group

Look-ahead and Look-behind

(?= ) : Look-ahead, Matches a group after the main expression without including it in the result

Author

Full Stack Web Developer Jr.

GitHub profile Rogers0404: https://github.com/rogers0404.

Try it : Try By yourself on regexr.com.

Check it live on Github live https://rogers0404.github.io/passwordValidation/.

Github Repository: [GIT: git@github.com:rogers0404/passwordValidation.git.

@luisenriquech
Copy link

Check this post, here I validate the minimum cases.

a capital letter
a lowercase
A number
A symbol
From 8 to 50 characters

https://luisenriquech.blogspot.com/2022/07/expresion-regular-contrasena.html

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment