Password Regex with optional Special Characters Symbols

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

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

So this is the complete regex below:

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

Broken down we have

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 number. 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 a number. 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 change of add any you wish.

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

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s