Monday 8 October 2012

Allow only numeric decimal value in textbox using ASP.NET RegularExpressionValidator control

We often get requirement to allow only numeric decimal value in textbox for validation purpose, And also we have to specify number of decimal places. We can use ASP.Net RegularExpressionValidator control for this. In this control we can specify regex expression for validation.

The regex expression is "^[+]?[0-9]\d{0,5}(\.\d{1,4})?%?$" .
This regex allow only positive numeric value of maximum six digit and maximum four digit decimal value. The maximum allowed value by this regex is "999999.9999". You can change this accroding to your requirement.

Here is example.
In this example we take one textbox , one RegularExpressionValidator  and one button control.
If we enter more than maximum allowed value it will raise error message at a time of click on button or focus moved out of that text box control.

ASPX Code : 
        Product Quantity :
        <asp:TextBox runat="server" ID="txtQty" Width="100px"></asp:TextBox>
        <asp:RegularExpressionValidator runat="server" ID="RegularExpressionValidator1" ControlToValidate="txtQty"
            ValidationGroup="VGCreateFormMaster" ErrorMessage="Only Integer value allow."
            ValidationExpression="^[+]?[0-9]\d{0,5}(\.\d{1,4})?%?$" Display="Dynamic" Style="color: Red"></asp:RegularExpressionValidator><br />
        <asp:Button runat="server" ID="Button1" Text="Click" OnClick="btnClick_Click" />

Output : 
Output
View article on "Allow only integer value in textbox using ASP.NET RegularExpressionValidator control". Click Here...
 
Note : Give Us your valuable feedback in comments. Give your suggestions in this article so we can update our articles accordingly that.

3 comments:

  1. You made some excellent points there. I does a research for the matter and identified most men and women will concur together with your site.

    ReplyDelete
    Replies
    1. Hi Evelyn,
      Thanks for your valuable comment.

      Delete
  2. This is a great article thanks for sharing this informative information.. I will visit your blog regularly for some latest post.

    ReplyDelete