Allow only numeric values into textbox in Asp.Net

In this post, we will discuss how to use regular expression validator to the textbox that will allow only numeric values into the 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 the 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 the user put a number and it did not show any error message.

Check out Best Alternative to InfoPath -> Try Now

free sharepoint training

SharePoint Online FREE Training

JOIN a FREE SharePoint Video Course (3 Part Video Series)

envelope
envelope

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →