rtp
rtp

Reputation: 794

Password Regular Expression Validation

These are the requirements but I guess it's too complicated for my regular expression skills...

. between 6 and 10 alphanum characters
. allowed A-Z,a-z,0-9,@,$,_
. Must begin with a letter
. Must contain at least one number
. cannot contain two consecutive identical characters
. cannot contain two consecutive identical numbers

I know the basic of regular expression such as [A-Za-Z] = characters only etc... but when it comes to consecutive character and stuff...

Upvotes: 2

Views: 7869

Answers (3)

Sedecimdies
Sedecimdies

Reputation: 152

should you want to validate the password you can use groups to do soo ;

(?<a>[a-zA-Z])?(?<b>[0-9])?(?<c>[@%$#/\\\(\)])?

Will give you a match in any of the 3 groups (a,b and c)

uper and lower characters will be in group a

numeric characters will be in group b

and special characters will be in group c

you can use the regex.match.groups("a").count to see if any characters from group a could be found

if you find characters in all 3 groups, the password is strong.

Upvotes: 0

Rohit Dubey
Rohit Dubey

Reputation: 1294

Try this

((?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[\W]).{6,20})

Description of above Regular Expression:

(           # Start of group
  (?=.*\d)      #   must contains one digit from 0-9
  (?=.*[a-z])       #   must contains one lowercase characters
  (?=.*[A-Z])       #   must contains one uppercase characters
  (?=.*[\W])        #   must contains at least one special character
              .     #     match anything with previous condition checking
                {6,20}  #        length at least 6 characters and maximum of 20 
)           # End of group

"/W" will increase the range of characters that can be used for password and pit can be more safe.

Upvotes: 1

rtp
rtp

Reputation: 794

        string pattern1 = @"^[a-zA-Z]([a-zA-Z])*"; //start and any number of characters
        string pattern2 = @"[0-9]+"; //one number or more numbers
        string pattern3 = @"[@#$%]*"; // special symbol allowed
        string pattern4 = @"(.)\1";//consecutive characters
        string pattern5 = @"^(.){6,10}$"; //min max

Upvotes: 0

Related Questions