Allow Only Numeric Values and allow Only one DOT in TEXTBOX using JavaScript

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
  1. <asp:ScriptManager runat="server" ID="scrp1"></asp:ScriptManager>  
  2.    
  3. <script type="text/javascript">  
  4.     var specialKeys = new Array();  
  5.   
  6.     specialKeys.push(8); //Backspace  
  7.   
  8.     function numericOnly(elementRef) {  
  9.   
  10.     var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ?    window.event.keyCode : -1;  
  11.   
  12.     if ((keyCodeEntered >= 48) && (keyCodeEntered <= 57)) {  
  13.   
  14.     return true;  
  15.   
  16. }  
  17.   
  18. // '.' decimal point...  
  19.   
  20. else if (keyCodeEntered == 46) {  
  21.   
  22. // Allow only 1 decimal point ('.')...  
  23.   
  24. if ((elementRef.value) && (elementRef.value.indexOf('.') >= 0))  
  25.   
  26.     return false;  
  27.   
  28. else  
  29.   
  30.     return true;  
  31.   
  32. }  
  33.   
  34.     return false;  
  35.   
  36. }  
  37. </script>  
Now Call this script on textbox
  1. < asp: TextBox ID = "txtUnitprice"  
  2. runat = "server"  
  3. TabIndex = "8"  
  4. Width = "120px"  
  5. onkeypress = "return numericOnly(this);"  
  6. ondrop = "return false;"  
  7. 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.