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 PRICE field in textbox. user not allow to enter string value and allow only numeric and it also allow only one DOT.
Please see this article in my blog.
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);
-
- function numericOnly(elementRef) {
-
- var keyCodeEntered = (event.which) ? event.which : (window.event.keyCode) ? window.event.keyCode : -1;
-
- if ((keyCodeEntered >= 48) && (keyCodeEntered <= 57)) {
-
- return true;
-
- }
-
-
-
- else if (keyCodeEntered == 46) {
-
-
-
- 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 query regarding this, Feel free to comment Me.