Regular Expression does not work correctly on webform

Asked

Viewed 269 times

1

I have the following field and validator in ASP.NET Webform

<asp:TextBox ID="txt_Senha" runat="server" CssClass="form-control" placeholder="Senha" TextMode="Password" AutoCompleteType="Disabled"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txt_Senha" ValidationExpression="(?=.{8,})[a-zA-Z]+[^a-zA-Z]+|[^a-zA-Z]+[a-zA-Z]+" ErrorMessage="A senha deve conter:<BR> * Mínimo 8 dígitos;<BR>* Números;<BR>* Letras MAIUSCULAS e minusculas" ForeColor="Red" />

When testing regular expression in: http://ferramentas.lymas.com.br/regexp/regexp_br.php

Works well, but on the page it does not accept the password:

1q2w3e4f5t6y7u8i

I believe it’s because there’s no Maluscula? But accept the password:

quadros2014

and quadros2014a unaccepted

2 answers

2

Situation

The reason why the password 1q2w3e4f5t6y7u8i not this working is the following, in your ER you have:

(?=.{8,})    // ate aqui ok, você limitou a ter pelo menos 8 caracteres
[a-zA-Z]+    // uma ou mais quantidades de letras (alpha)
[^a-zA-Z]+   // qualquer coisa que não seja alpha, ou seja, ate mesmo estes caracteres "&¨#%"

validations:

stake%     // OK
stake4¨%   // OK
alpha!-=   // OK
casa¿@     // OK
casa$%dele // falso, pois tem 'dele' (alpha), apos [^a-zA-Z]+ (qualquer coisa exceto alpha)

after you have a |, or another possible validation:

(?=.{8,})    // ate aqui ok, você limitou a ter pelo menos 8 caracteres
[^a-zA-Z]+   // qualquer coisa que não seja alpha, ou seja, ate mesmo estes caracteres "&¨#%"
[a-zA-Z]+    // uma ou mais quantidades de letras (alpha)

validations:

%$#stake    // OK
@#$¨CASA    // OK
!varchar    // OK
teste3      // falso, pois inicio com 'teste' (alpha), primeira validacao [^a-zA-Z]+ (qualquer coisa exceto alpha)

Suggestion

To validate passwords alnum utilize:

(?=.{8,})[a-zA-Z0-9]+

validations:

1q2w3e4f5t6y7u8i  // OK
TESTE4de8         // OK
$varchar          // falso, uso de caracter `$` nao `alnum`
_cadaDele         // falso, uso de caracter `_` nao `alnum`

or if you want to release the _, simply: (?=.{8,})\w+.

  • Just to add, I’d do it this way (?=.{8,})([a-zA-Z0-9]+), add parentheses to catch the group.

  • 1

    I didn’t remember either (?i) when I did my answer, summarizing the string to (?i)(?=.{8,})([a-z0-9]+)

  • I liked your answer +1. I think the AP wants to force at least one of each type, in their response cases without uppercase pass. Let’s see what he really wants when he responds/gives feedback.

  • Thank you all, really needed minimum 8 characters, a minuscule, a capital case, a number and that accepts special characters but is not mandatory.

  • @Dorathoto, because of your needs, I believe the correct answer is Sergio’s. Mine makes the validation correct but does not contemplate the need for a character of each type. = D

1


I suggest using ^(?=.{8,})(?=.+[a-z])(?=.+[A-Z])(?=.+\d).+$

That is to say:

  • (?=.{8,}) to guarantee 8 characters.
  • (?=.+[a-z]) to ensure at least a small font character.
  • (?=.+[A-Z]) to ensure at least one large letter character.
  • (?=.+\d) to ensure at least one character that is a digit.
  • You can put the (?i) to ignore capital letters or minuscule, ^(?i)(?=.{8,})(?=.+[a-z])(?=.+\d).+$

  • @Guilhermelautert I think the AP really wants to distinguish between capital/minuscule. Our answers differ there. AP will tell you which one you want.

  • Just a doubt Sergio, I noticed that this expression asks 2 Maiusculas, and one would be enough...

  • @Dorathoto the expression accepts two or more capital letters but must be at least 1.

  • @Sergio but apparently in my code she is needing 2 capital letters to free the access. if the regular expression is correct I will check what else can be..

  • https://www.regex101.com/ said that when the capital letter is the first letter it does not validate.

  • @Dorathoto repara na ultima letra (maiuscula): https://www.regex101.com/r/iF8vZ6/1 dá match. And with a small letter, https://www.regex101.com/r/eQ0yH5/1, it says "Your Pattern does not match the Subject string.".

  • the password: Mac456789a does not validate, even if it has an upper case. https://www.regex101.com/r/eQ0yH5/2

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.