Thursday 4 October 2012

Allow only integer value in textbox using ASP.NET RegularExpressionValidator control

For validation purpose in your project at some places you put text boxes that accept only integer value i.e. 1, 20 , 50, 200 etc... To put validation on that text boxes and allow only integer value you can use ASP.NET Regular Expression Validator Control. In this control we make one regex expression and do validation for that text box.
The regex expression is "^\d+$" .

Here is example.

In this example we take one textbox , one RegularExpressionValidator  and one button. In this textbox we allow only integer value. If we insert none integer value and click on button at that time validator control display error message.

ASPX Code :
        Product Quantity :
        <asp:TextBox runat="server" ID="txtQty" Width="50px"></asp:TextBox>
        <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1" ControlToValidate="txtQty"
            ValidationGroup="VGCreateFormMaster" ErrorMessage="Only Integer value allow."
            ValidationExpression="^\d+$" Display="Dynamic" Style="color: Red"></asp:RegularExpressionValidator><br />
        <asp:Button runat="server" ID="Button1" Text="Click" OnClick="btnClick_Click" />

Output : 



Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.

1 comment:

  1. Allow only integer value in textbox using ASP.NET RegularExpressionValidator control it's Really cool dude.

    ReplyDelete