简体   繁体   English

在ASP.NET文本框中仅允许数字或十进制数据

[英]Allow only numeric or decimal data in asp.net textbox

I have a textbox in asp.net in which I want to enter only either numeric or decimal data. 我在asp.net中有一个文本框,我只想输入数字或十进制数据。 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) 我修改了以下代码,以便允许小数点(句点)(代码= 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). 但这并不能很好地工作(此处我将190用作定义的标准时间)。 Moreover If someone can help me limit this textbox for user to enter only 6 characters.Can anyone please correct me. 此外,如果有人可以帮助我限制此文本框,以便用户只能输入6个字符。有人可以纠正我。 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>

You can use following as 您可以使用以下作为

In ASPX: 在ASPX中:

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

Javascript: 使用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+. 您的正则表达式不正确,应该是^ \\ d + $而不是\\ d +。

You might not have hooked up keyup event that is why above one is not working. 您可能没有连接过keyup事件,这就是上面的一个不起作用的原因。

But regex will work alone. 但是正则表达式将单独运行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM