Hey peoples, im a complete newbie to ASP.NET. Basically i just need to validate a textbox so that only a 4 digit number can be inserted. Wat code should i use (im using VB as my source language and it's not a code-behind). This is the asp textbox validation attributes:

<asp:CustomValidator 
                ID="lengthCheck" 
                runat="server" 
                ErrorMessage="Must be at least 4 digits"
                ControlToValidate="postcode"
                OnServerValidate="pcode"
                Text="Must contain 4 digits"
                Display="Dynamic">
</asp:CustomValidator>

Thanks heaps

Well, checking length in asp.net is not quite easy. But you can restrict user from entering characters more that allowed limit.

set the MaxLength property of textbox to the number of characters you want to allow.

Well the draw back of this method is that, it does not display any error to the user that they are entering more characters than allowed.

But you will not need to display. the error.

I did this several times and it is working.

or you can use javascript to validate.

I would suggest using the RangeValidator so that you could set the range as MaximumValue="9999" MinimumValue="1111" Type="Integer"

You can use RegularExpressionValidator for accepting only four digits and RequiredFieldValidator for mandatory checking.

Hope this sample code will help you.

<asp:TextBox ID="txtPostCode" runat="server"></asp:TextBox>
    <asp:RegularExpressionValidator ID="regExpPostCode" runat="server" ControlToValidate="txtPostCode" ErrorMessage="Must be at least 4 digits" ValidationExpression="\d{4}">
   </asp:RegularExpressionValidator>
   <asp:RequiredFieldValidator ID="reqFieldPostCode" runat="server" ControlToValidate="txtPostCode" ErrorMessage="Post Code must be entered">
  </asp:RequiredFieldValidator>

Thanks all...they are all very good ideas. I'm sure u will see me posting more questions in the near future haha

This question has already been answered. Start a new discussion instead.