Question

I'm trying to validate a TextBox used for the username in the user registration page. The conditions are:

  1. First character should be an uppercase letter.
  2. Length should be a maximum of 8 characters.

I'm using a RegularExpressionValidator to check both conditions at the same time but this results in the same error message: "Username should be maximum 8 characters long and start with an uppercase letter.".

I would like to display specific error messages based on the above conditions, say:

  • Condition 1: Username should start with an uppercase letter.
  • Condition 2: Username should be maximum 8 characters long.

I want both error messages to appear if both conditions are not satisfied.

Here is my present code:

<asp:TextBox ID="username" runat="server" CssClass="InputText"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUsername" runat="server"
    ControlToValidate="username" Display="Dynamic"
    EnableClientScript="true">Username is required
</asp:RequiredFieldValidator>
<asp:RegularExpressionValidator
    ID="revUsername" runat="server" Display="Dynamic"  
    ControlToValidate="username"
    ValidationExpression="[A-Z][A-Z0-9]{0,7}" >
        Username should be max 8 characters long
        and should start with an alphabet.
</asp:RegularExpressionValidator>

How do I achieve this?

Was it helpful?

Solution

<asp:TextBox ID="username" runat="server" MaxLength="8" CssClass="InputText"></asp:TextBox>
<asp:RequiredFieldValidator ID="rfvUsername" runat="server"
    ControlToValidate="username" Display="Dynamic"
    EnableClientScript="true">Username is required
</asp:RequiredFieldValidator>

for first letter uppercase Regex to check if the first character is uppercase

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top