Password Regex with optional Special Characters Symbols

After number of failed attempts I have gotten to the Regex that validates your password with optional symbols. This example will validate it has 1 lowercase letter, 1 uppercase letter, 1 number and then the symbols are optional, but it also validates the symbols added in are acceptable.

For example if I entered a password ‘Password1!’ this would pass as I have 1 of each and my symbol is valid. However if I put in ‘Password1£’ this would then fail.

The Guide to the success

I have broken down the completed regex into what each part does, so you can further understand the mechanics.

To validate the password is between 8 and 12 characters. In order, it states that the following is mandatory(‘?=’), from the beginning of the string(‘^’), check length minimal 8 and longest 12(‘{8,12}’), to the end of string($).

(?=^.{8,12}$)

This will then check that the string has a number. The following is mandatory (‘?=’) and must contain 1 or more (‘*’) digits in the string (‘d’)

(?=.*\d)

This will then check that the string has a lowercase letter. The following is mandatory (‘?=’) and must contain 1 or more (‘*’) lowercase letter from a-z in the string (‘[a-z]’)

(?=.*[a-z])

This will then check that the string has an uppercase letter. The following is mandatory (‘?=’) and must contain 1 or more (‘*’) uppercase letter from A-Z in the string (‘[A-Z]’)

(?=.*[A-Z])

Finally it will check there is no whitespaces in the string either. There must not (‘?!’) contain 1 or more (‘*’) whitespaces in the string (‘s’)

(?!.*\s)

The last part then basically has everything that is valid in the string you are passing. Within the range of 0-9, a-z, A-Z and finally you have the valid symbols you can use. These are all backslash escaped just in chase for security. There are these symbols ‘! * ^ ? ] [ + – _ @ # $ % &’  but you can remove or add any you wish.

[
0-9
a-z
A-Z
!*^?](?=.*[\+\-\_\@\#\$\%\&])

The Completed Stage

(?=^.{8,12}$)(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?!.*\s)[0-9a-zA-Z!*^?](?=.*[\+\-\_\@\#\$\%\&])

2 thoughts on “Password Regex with optional Special Characters Symbols

Leave a message please

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.