Text field checker for minimum length

<asp:RequiredFieldValidator ID="NewPasswordRequired" runat="server" ControlToValidate="NewPassword" ErrorMessage="New Password is required." ToolTip="New Password is required." ValidationGroup="ChangeUserPasswordValidationGroup"> </asp:RequiredFieldValidator> 

How can I check the text box to enter a value whose length must be greater than 8 and must contain 1 number and 1 capital letter.

+4
source share
4 answers

In addition to your RequiredFieldValidator, add the RegularExpressionValidator parameter

For a regex pattern, you can use this pattern:

 ^.*(?=.{8,})(?=.*\d)(?=.*[az])(?=.*[AZ])(?=.*[@#$%^&+=]).*$ 

Must be at least 8 characters Must contain at least one lowercase letter, one uppercase letter, one digit and one special character Allowed special characters: @ # $% ^ & + =

Technically, you can only use the Regex validator, but using multipliers, you can have different error messages depending on a missing or simply incorrect password.

+4
source

I would use two validators: LenghtValidator and RegularExpressionValidator .

The advantage of having two validators is that you can have two error messages.

+2
source
 <asp:RegularExpressionValidator ID="RegExp1" runat="server" Erenter code hererorMessage="Password length must be between 7 to 10 characters" ControlToValidate=" txtPassword " ValidationExpression="^[a-zA-Z0-9'@&#.\s]{7,10}$" /> 
+1
source

You must use two separate validators, one for checking the length and the other for checking the minimum required characters (this way you will get two separate error messages for each check).

Example:

 <asp:TextBox ID="MyPasswordField" runat="server" TextMode="Password"></asp:TextBox> <!-- Add validator for minimum length requirement --> <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="MyPasswordField" ID="MyPassordMinLengthValidator" ValidationExpression="^[\s\S]{8,}$" runat="server" ErrorMessage="Password must be at least 8 characters long."></asp:RegularExpressionValidator> <!-- Add validator for minimum required characters --> <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="MyPasswordField" ID="MyPassordRequiredCharacterValidator" ValidationExpression="^(?=.*[az])(?=.*[AZ])(?=.*\d).+$" runat="server" ErrorMessage="Must contain at least one uppercase letter, one lowercase letter, and one number."></asp:RegularExpressionValidator> 

If you also need to check the password for the maximum length, you can change the regular expression for the first validator as follows:

 <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="MyPasswordField" ID="MyPassordMinMaxLengthValidator" ValidationExpression="^[\s\S]{8,40}$" runat="server" ErrorMessage="Password must be between 8 and 40 characters long."></asp:RegularExpressionValidator> 

If you need to verify that at least one special character is present, you can use this regular expression for the second check (at least one of the following characters: <space> @ # $% ^ &? ;;: _ | {} = + - * () [] ~) :.

 <asp:RegularExpressionValidator Display="Dynamic" ControlToValidate="MyPasswordField" ID="MyPassordRequiredCharacterValidator" ValidationExpression="^(?=.*[az])(?=.*[AZ])(?=.*\d)(?=.*[@#$%^&?,;:_|~{}=\+\-\!\.\*\(\)\[\] ]).+$" runat="server" ErrorMessage="Must contain at least one uppercase letter, one lowercase letter, one number, and one special character."></asp:RegularExpressionValidator> 
0
source

All Articles