Introduction
In this article, we will see how to Allow only Numeric value and Only one DOT in Textbox. This example will helpful when we want to use the PRICE field in the textbox. user is not allowed to enter string value and allow only numeric and it also allows only one DOT.
Please see this article on my blog.
Using Code :
Write this script in Page Design Source under <Body> tag
- <asp:ScriptManager runat="server" ID="scrp1"></asp:ScriptManager>
- <script type="text/javascript">
- var specialKeys = new Array();
- specialKeys.push(8); //Backspace
- function numericOnly(elementRef) {
- var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ? window.event.keyCode : -1;
- if ((keyCodeEntered >= 48) && (keyCodeEntered <= 57)) {
- return true;
- }
- // '.' decimal point...
- else if (keyCodeEntered == 46) {
- // Allow only 1 decimal point ('.')...
- if ((elementRef.value) && (elementRef.value.indexOf('.') >= 0))
- return false;
- else
- return true;
- }
- return false;
- }
- </script>
Now Call this script on textbox
- < asp: TextBox ID = "txtUnitprice"
- runat = "server"
- TabIndex = "8"
- Width = "120px"
- onkeypress = "return numericOnly(this);"
- ondrop = "return false;"
- onpaste = "return false;" > < /:TextBox>
Everything is done and set.
I hope you liked my article. If you have any queries regarding this, Feel free to comment on Me.

TEMESEGAN WALELIGNPosted Dec 29, 2020, 8:26 AM
<input type="text" class="decimalnumber" value="" /> $('.decimalnumber').keyup(function(){ var val = $(this).val(); if(isNaN(val)){ val = val.replace(/[^0-9\.]/g,''); if(val.split('.').length>2) val =val.replace(/\.+$/,""); } $(this).val(val); });?
RahulPosted Jan 17, 2020, 4:06 PM
Have you ever tried this code in mobile? It doesn't work
Manav PandyaPosted Aug 11, 2017, 3:05 AM
Its not working with Mozilla
Manav PandyaPosted Aug 11, 2017, 2:58 AM
Nice one ............
Nikunj TrivediPosted Aug 8, 2015, 3:44 AM
Thank you Ranjan Sir
Ranjan SenapatiPosted Jul 31, 2015, 8:54 AM
good one.....