Numeric Number Only TextBox JavaScript RegularExpression Validator

In this example i am explaining how to create Numeric Number Only Textbox Using JavaScript Or Regular Expression Validator which accept only numbers in web page.

Number only textbox using javascript or regularexpressionvalidator

1. Number only textbox using javascript.
Go to html source of aspx page and write below mentioned script in head section of page.

Call this function in onKeyPress event of textbox.
Write this code in html source of textbox.

<asp:TextBox ID="TextBox2" runat="server" 
             onKeyPress="return numberOnlyExample();">

We can also do this programmetically in code behind like this.

on Page_Load event of page add onKeyPress attribute to textbox and call the function to accept only numerics.

protected void Page_Load(object sender, EventArgs e)
       TextBox2.Attributes.Add("onkeypress", "return ((window.event.keyCode >= 48 && window.event.keyCode <= 58))");

2. Creating Numeric or Number only textbox using Regular Expression Validator.
Drag and place RegularExpressionValidator control on aspx page and set it's properties as mentioned below in html code.

Set ControlToValidate property to textbox1.
Set ValidationExpression.
Set ErrorMessage.

HTML Source
     ErrorMessage="Please Enter only Numbers" 
Hope this helps
If you like this post than join us or share


dsr4 said...

thank you for sharing . it's a good articel.i will keep focus on your blog

Anonymous said...

thank u for sharing your information. i have found regular expression checking using below link.

Vee Eee Technologies said...

Excellent pieces. Keep posting such kind of information on your blog. I really impressed by your blog.
Vee Eee Technologies

Anonymous said...

Thank you... Nice post.

Find More Articles