In this post we will discuss how use regular expression validator to textbox that will allow only numeric values into textbox in Asp.Net.
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:
<div>
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" />
</div>
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.
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:
<div>
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" />
</div>
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.


No comments:
Post a Comment