2

I have a textbox in asp.net in which I want to enter only either numeric or decimal data. The user should not be allowed to even enter any other data. I have tried it myself using the following code. But it does not allow user to enter decimal point for decimal data.

<script language="JavaScript">
function onlyNumbers(evt)
{
    var e = event || evt; // for trans-browser compatibility
    var charCode = e.which || e.keyCode;

    if (charCode > 31 && (charCode < 48 || charCode > 57))
        return false;

    return true;
}
</script>

I had modified the code as below so that it allows the decimal point (period) (code=190)

if (charCode != 190) 
{ ((charCode > 31 && (charCode < 48 || charCode > 57)))
 return false; }
 return true;

But it doesn't work as well (Here i used 190 for period as defined standard). Moreover If someone can help me limit this textbox for user to enter only 6 characters.Can anyone please correct me. I will be really thankful.

Note: I had also tried this. It also works only for numbers and also allows entering of non-numeric data but displays an error message if non-numeric data is entered. But its not what I want. I don't want user to even enter non-numeric data.

<asp:TextBox ID="txtBasicPay" runat="server" Height="15px" Width="120px"></asp:TextBox>
<asp:RegularExpressionValidator ID="RegularExpressionValidator1" ControlToValidate="txtBasicPay" runat="server" ErrorMessage="Only numbers allowed" ValidationExpression="\d+"></asp:RegularExpressionValidator>
2
  • What happens when you try the Javascript? Does it allow the wrong characters? Have you got the aspx for the JavaScript version? Commented Oct 12, 2015 at 14:37
  • @DanielCasserly The JavaScript version works. It allows only numeric data and doesn't allow any other data but I also want it to allow Period (.) that's why I had modified the code and added another "if" condition to allow Period (code=190) but the code doesn't allow anything in my modified version. Here is the textbox that I am using it with <asp:TextBox ID="txtBasicPay" runat="server" Height="15px" Width="120px" onkeypress="return onlyNumbers();"></asp:TextBox> Commented Oct 12, 2015 at 14:47

2 Answers 2

5

You can use following as

In ASPX:

<asp:TextBox ID="txtBasicPay" runat="server" Height="15px" Width="120px" onkeypress="return isFloatNumber(this,event);"></asp:TextBox>

Javascript:

function isFloatNumber(e, t) {
   var n;
   var r;
   if (navigator.appName == "Microsoft Internet Explorer" || navigator.appName == "Netscape") {
      n = t.keyCode;
      r = 1;
      if (navigator.appName == "Netscape") {
         n = t.charCode;
         r = 0
      }
   } else {
      n = t.charCode;
      r = 0
   }
   if (r == 1) {
      if (!(n >= 48 && n <= 57 || n == 46)) {
         t.returnValue = false
      }
   } else {
      if (!(n >= 48 && n <= 57 || n == 0 || n == 46)) {
         t.preventDefault()
      }
   }
}
Sign up to request clarification or add additional context in comments.

4 Comments

This code snippet is too old. I used it a few years ago, it does not currently handle Paste events (Ctrl+V), but you can modify it to handle this scenario too.
Thank You Haraman. Works great. Just what I needed. I will now try to limit this textbox to allow only six characters. Thanks again
For char limits you can just use MaxLength="6" attribute of asp:TextBox
Sure thing. Thank you :)
0

Your regex is incorrect , It should be ^\d+$ not \d+.

You might not have hooked up keyup event that is why above one is not working.

But regex will work alone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.