Powered by Blogger.

Saturday, February 15, 2014

Allow only numeric values into textbox in Asp.Net

In this post we will discuss how use regular expression validator to textbox that will allow only numeric values into textbox in Asp.Net.

Also you can check out my previous posts on:

- NuGet tutorial in Asp.Net

- How to display serial number automatically in GridView in asp.net?

- Transforms in WPF and their types

Through regular expression you can allow user to enter only numeric values into the textbox.

The regular expression is "\d+".

Below is the full code:

            Enter Number:
            <asp:TextBox ID="txtNumber" runat="server"></asp:TextBox><br />
            <asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="txtNumber" ErrorMessage="Please enter only numeric value" ValidationExpression="\d+" ForeColor="Red"></asp:RegularExpressionValidator>
            <br />
            <asp:Button ID="btnSumbmit" runat="server" Text="Submit" />

Check the below fig, here user wanted to enter a letter and it shows the error message.

Below is the fig where user put a number and it did not show any error message.