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. <script type="text/javascript">
  3. var specialKeys = new Array();
  4. specialKeys.push(8); //Backspace
  5. function numericOnly(elementRef) {
  6. var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ? window.event.keyCode : -1;
  7. if ((keyCodeEntered >= 48) && (keyCodeEntered <= 57)) {
  8. return true;
  9. }
  10. // '.' decimal point...
  11. else if (keyCodeEntered == 46) {
  12. // Allow only 1 decimal point ('.')...
  13. if ((elementRef.value) && (elementRef.value.indexOf('.') >= 0))
  14. return false;
  15. else
  16. return true;
  17. }
  18. return false;
  19. }
  20. </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.