In this example i am explaining how to create Numeric Number Only Textbox Using JavaScript Or Regular Expression Validator which accept only numbers in asp.net web page.
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.
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.
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
Hope this helps
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();"> </asp:TextBox>
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
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server" ControlToValidate="TextBox1" ErrorMessage="Please Enter only Numbers" ValidationExpression="\d+"> </asp:RegularExpressionValidator>
If you like this post than join us or share
3 comments:
thank you for sharing . it's a good articel.i will keep focus on your blog
thank u for sharing your information. i have found regular expression checking using below link.
http://jquery-javascript-code.blogspot.com/2011/12/regular-expression-validator-online-for.html
Thank you... Nice post.
Post a Comment