-3

i need check before save, if a textbox (ASP.NET) for password have at least

one or more char in lowercase regardless order one or more char in uppercase regardless order one or more number regardless order one or more of this special char " ! # $ % & % ' () * ... and minimum length o 8 char

How i can do that with Regex Expression ?

ex: #Abc123aB (valid)
    abcAbc123aB123&  (valid)
    abc123abc (invalid) missing uppercase and special char

Edited

<tr>
  <td>Password</td>
  <td><asp:TextBox ID="txtClave" TextMode="Password"  runat="server" > </asp:TextBox>&nbsp; 
  <div id="divmensaje" style="display:inline-block">
  <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtClave" ValidationExpression="^[A-Za-z0-9_\.\*#]{8,25}$" ErrorMessage="Wrong format"></asp:RegularExpressionValidator>
  </div> 
  </td>                
</tr>

thanks in advance for you time and support

George Poliovei
  • 1,009
  • 10
  • 12
  • I edited the post and thank you, with this form abc123# text is valid but it should not be because lack a capital letter, i will try Nefarii answer from server side check – George Poliovei Nov 09 '15 at 18:54
  • Possible duplicate of [Regex for strong password in ASP.net](http://stackoverflow.com/questions/22232582/regex-for-strong-password-in-asp-net) – Mariano Nov 10 '15 at 07:09

2 Answers2

0

Although it is possible to do it, I wouldn't try to resolve it with a single regex. What's the point in avoiding code lines? The code gets much more complex and I believe it would also be slower.

I would check the length using .Length (way faster than using regex) and then, small and simple regex to check each char type.

Diego
  • 16,436
  • 26
  • 84
  • 136
  • Thank you for you time, and i will keep present your recommendation. i made many tries and just made a question here to know if it is possible. is not my intention to bother anyone with questions that perhaps are very basic to them – George Poliovei Nov 09 '15 at 19:02
  • You're welcome. There is no problem with basic question but you should always post what you have tried or why the results of some research didn't help you. – Diego Nov 09 '15 at 19:25
0
String password = "Aa!1asdetD"
HashSet<char> specialCharacters = new HashSet<char>() { '!', '@', '%', '$', '#' };
if (password.Any(char.IsLower) && //Lower case 
     password.Any(char.IsUpper) &&
     password.Any(char.IsDigit) &&
     password.Length > 8
     password.Any(specialCharacters.Contains))
{
  //valid password
}

Taken form Regex for strong password in ASP.net

Also, I thought I would post this - it took a little finagling, but I believe this should also accomplish what you want

^(?=.{8,})(?=.*[a-z])(?=.*[A-Z])(?=.*[0-9])(?=.*\W).+$
Community
  • 1
  • 1
Nefariis
  • 3,451
  • 10
  • 34
  • 52