Click here to Skip to main content
15,891,375 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I need to have a regular expression to check if the password has atleast one character and one special character anywhere in the password. Hence i used the below regular expression,

C#
if (!System.Text.RegularExpressions.Regex.IsMatch(a, "^(?=.*[#@].*)(?=.*[a-zA-Z].*)[a-zA-Z0-9#@]{6,15}$"))
{...}


But this regular expression doesnot allow the special character to be last of the password. Say for example, it allows test!6767 but it doesnot allow test6767!.

Please tell me what i change i should do in the external expression

Thanks
Posted
Updated 13-Sep-11 6:17am
v2

1 solution

From my answer to the question How to verify a password which contain .......[^]
(?=^.{8})(?=.*[a-z])(?=.*[A-Z])(?=.*[^\w\d])(?=.*\d).*$


(?=^.{8})
The minimum length

(?=.*[a-z])
Verify atleast one lowercase character

(?=.*[A-Z])
Verify atleast one uppercase character

(?=.*[^\w\d])
Verify atleast one special character

(?=.*\d)
Verify atleast one digit
 
Share this answer
 
v3
Comments
Simon Bang Terkildsen 13-Sep-11 12:18pm    
OP wrote
For the special character check my regular expression works too and so is your solution. But my issue is when the special character is at the last of the password then validation fails. Say if password is test!6767 then it validates as correct password. But when the password is test6767! it says as wrong password. But it is not.
Simon Bang Terkildsen 13-Sep-11 12:22pm    
I've just tested your regex and you state that it matches test!6767.. It does NOT. You specifically state which special characters you allow and you do NOT allow !
Which is also why it doesn't match test6767!
Simon Bang Terkildsen 13-Sep-11 12:20pm    
Yeah I get that you've written your regex that works and then not entirely, my answer was not supposed to be a "here is the solution to all your problems"-answer but to allow you to find your mistake yourself.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900