最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Allow only numeric or decimal data in asp.net textbox - Stack Overflow

programmeradmin1浏览0评论

I have a textbox in asp 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 patibility
    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>

I have a textbox in asp 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 patibility
    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>
Share Improve this question edited Oct 12, 2015 at 15:09 Morpheus 1,6341 gold badge21 silver badges32 bronze badges asked Oct 12, 2015 at 14:34 Rizwan606Rizwan606 6672 gold badges9 silver badges22 bronze badges 2
  • What happens when you try the Javascript? Does it allow the wrong characters? Have you got the aspx for the JavaScript version? – Daniel Casserly 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> – Rizwan606 Commented Oct 12, 2015 at 14:47
Add a ment  | 

2 Answers 2

Reset to default 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()
      }
   }
}

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.

发布评论

评论列表(0)

  1. 暂无评论