Click here to Skip to main content
15,890,579 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to validate password in Alphanumeric Format with one numeric and one special character at minimum.in asp.net with c#. I am able to validate the alphanumeric format but i need to validate
Alphanumeric with one numeric and one special character at minimum. Code for the same is updated below


What I have tried:

#region IsAlphaNumeric
      public static bool IsAlphaNumeric(string inputAlphabet)
      {
         const string expression = @"^[A-Za-z0-9]+$";

          Regex regex = new Regex(expression);
          return regex.IsMatch(inputAlphabet);
      }

      #endregion
Posted
Updated 24-Oct-21 22:42pm
v2

Presumably at least one numeric and at least one special character?

Also, if you're allowing special characters, it's not going to be "alphanumeric".

This pattern validates:
  • at least one upper-case letter;
  • at least one lower-case letter;
  • at least one number;
  • at least one "special character"; and
  • minimum length of 10;

RegEx
(?=.*[A-Z])(?=.*[a-z])(?=.*\d)(?=.*[^\w\s])^.{10,}$
Zero-Width Positive Lookahead Assertions | Microsoft Docs[^]
 
Share this answer
 
Comments
ranio 25-Oct-21 5:47am    
will below Regex Pattern works for the Password to contain at least one number;
at least one "special character and length of 7
(?=.*)(?=.*)(?=.*\d)(?=.*[^\w\s])^.{7,}$
Richard Deeming 25-Oct-21 5:48am    
Take the two (?=.*) groups out, since they don't do anything. Otherwise, it should work as you describe.
A regex is a poor tool for password validation - it's a text processor not a semantic analysis tool.
If you want to count items, then use multiple regexes:
[A-Za-Z]

\d

[!"£$%^&*()-_=+]

And use the Regex.Matches[^] method to return a MatchCollection for each. You can then use it's Count property to tell you how many there are: 0 is bad, non-zero in all of them is OK.

But ... enforcing password strength is normally a poor idea: it reduces security as it either encourages people to write it down, or to use the same password for all logins.
Why enforced password complexity is worse for security (and what to do about it)[^]
 
Share this answer
 

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