2

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?

KavenG
  • 161
  • 1
  • 9
hima
  • 610
  • 3
  • 10
  • 24
  • And having 2 separate validators attached to the same element doesn't work for you? – Icarus Aug 01 '13 at 13:43
  • no it does not. I even tried adding CustomValidator for the second condition even then it does not give required output. – hima Aug 01 '13 at 13:46
  • It works for me. I just tested it. If I submit the form leaving the username field empty I get: `Username is required`. If i enter something and submit the form, I get: `Username should be max 8 characters long and should start with an alphabet.` – Icarus Aug 01 '13 at 13:47
  • sorry you got me wrong. I want to display 2 different error messages. Please read the question carefully. Hope my question is clear. – hima Aug 01 '13 at 13:49
  • You want to **display both error messages when the username field is empty**, then. The way you phrased your question does not express this clearly. – Icarus Aug 01 '13 at 13:58
  • Oh no. When condition 1 fails display: Username should start with and alphabet. When condition 2 fails display: Username should be max 8 characters. – hima Aug 01 '13 at 14:00
  • that's exactly what I tested and it works fine (at least on my PC). What error, if any, do you get specifically? What do you see when you run the code you posted? – Icarus Aug 01 '13 at 14:15
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/34634/discussion-between-hima-and-icarus) – hima Aug 01 '13 at 14:23

1 Answers1

1
<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

Community
  • 1
  • 1
senthilkumar2185
  • 2,536
  • 3
  • 22
  • 36