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"

just set the maxlength of the textbox to 4 and use a range validator..that should solve your problem.

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

mark it as solved if u found the solution..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.