Regular expression to check special character
-
I need to check whether password has at least 6 characters with at least 1 letter and one special character. I am checking the condition !Regex.IsMatch(strPassword,"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,24})$"). But it doesnot allow special character. I need to change the expression to check if there is at least one special character. Please help me out.
-
I need to check whether password has at least 6 characters with at least 1 letter and one special character. I am checking the condition !Regex.IsMatch(strPassword,"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,24})$"). But it doesnot allow special character. I need to change the expression to check if there is at least one special character. Please help me out.
In .NET regex language there is a character class "\W" that represents "non-word characters" (i.e. characters other than letters, digits, or underscores). You could also replace your [0-9] with "\d" (note the lower case). P.S. I think this question may be better suited for the regular expressions forum.
-
I need to check whether password has at least 6 characters with at least 1 letter and one special character. I am checking the condition !Regex.IsMatch(strPassword,"(?!^[0-9]*$)(?!^[a-zA-Z]*$)^([a-zA-Z0-9]{6,24})$"). But it doesnot allow special character. I need to change the expression to check if there is at least one special character. Please help me out.
I am assuming you consider # and @ to be the allowable special characters:
^(?=.*[#@].*)(?=.*[a-zA-Z].*)[a-zA-Z0-9#@]{6,24}$
Anything that matches that will be a valid password (between 6 and 24 characters, contains only letters/numbers/special characters, contains at least one letter, and contains at least one special character).
Martin Fowler wrote:
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
-
I am assuming you consider # and @ to be the allowable special characters:
^(?=.*[#@].*)(?=.*[a-zA-Z].*)[a-zA-Z0-9#@]{6,24}$
Anything that matches that will be a valid password (between 6 and 24 characters, contains only letters/numbers/special characters, contains at least one letter, and contains at least one special character).
Martin Fowler wrote:
Any fool can write code that a computer can understand. Good programmers write code that humans can understand.
Thank you