如何限制用户仅输入数字值 [英] HOW to restrict the user to enter only numaric value

查看:77
本文介绍了如何限制用户仅输入数字值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我的代码有问题.
我已经使用javaScript开发了一个代码,该代码仅允许在文本框中输入数字值,但是如何在on click事件上对其进行定义?
甚至我们都知道文本框没有单击事件,请帮帮我.

在此先感谢
这是代码.

Hi Guys,

There is a problem in my code.
I have developed a code using javaScript that allows only numeric value to enter in text box, but how can I define it on the on click event ?
Even we know that Text Box does not has on click Event Please help me out.

Thanks in advance
Here is the code.

function NumberOnly()
{
    var AsciiValue = event.keyCode;

    if ((AsciiValue >= 48 && AsciiValue <= 57) || (AsciiValue == 8 || AsciiValue == 127))
       event.returnValue = true;
    else
       event.returnValue = false;
}


<input type = "text" id = "text1"  önkeypress = "return NumberOnly()" >

推荐答案

以下是我使用过的最简单的示例中最简单的一个.
Here is the simplest of simplest examples I''ve used plenty.
<html>
<head>
   <script type="text/javascript">
   function onYourControlKeyDown(e){	
   // Arrows, backspace, tab, shift, control, end, home, delete
   if ((e.keyCode >= 37 && event.keyCode <= 40) || e.keyCode == 8 || e.keyCode == 9 ||
      e.keyCode == 16 || e.keyCode == 17 || e.keyCode == 35 || e.keyCode == 36 || e.keyCode == 46) {
      return true;
      }
   // Numbers are awesome...
   if ((e.keyCode >= 48 && e.keyCode <= 57 && e.shiftKey == false) ||
      (e.keyCode >= 96 && e.keyCode <= 105 && e.shiftKey == false)) {
      return true;
   }	
   e.preventDefault();
   return false;
   }
   </script>
</head>
<body>
   <input type='text' onkeydown='onYourControlKeyDown(window.event);' />
</body>
</html>


看看这个jquery插件,我已经使用了几次,效果很好. http://www.decorplanit.com/plugin/ [
Take a look at this jquery plugin, i''ve used it a few times and it works pretty good. http://www.decorplanit.com/plugin/[^]

One other thing I would do is validate the control server side as well. A user can always by pass your validation client side with firebug or some similiar debugging tool so once the page is posted back just do a double check on the control that the textbox value is actually an integer.

int.TryParse(txtBox.Value, out tmpInt)



希望对您有帮助



Hope that helps


您已在JavaScript中使用了event.keyCode,但尚未将任何事件传递给该函数.

您必须像下面这样.
önkeypress = "return NumberOnly(event)"

如果我修改您的代码,则它将如下所示.
You have used event.keyCode inside the javaScript, but you have not passed any event to the function.

You have to do like below.
önkeypress = "return NumberOnly(event)"

If I modify your code then it will look like below.
<HTML>
   <HEAD>
   <SCRIPT language="Javascript">

      function NumberOnly(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if (charCode > 31 && (charCode < 48 || charCode > 57))
            return false;

         return true;
      }

   </SCRIPT>
   </HEAD>
   <BODY>
      <INPUT id="text1" onkeypress="return NumberOnly(event)" type="text" name="text1">
   </BODY>
</HTML>


它将为您工作.

注意:请更改您的命名约定.不要将id写为 text1 ,而是写 txtNumber .对于函数,不要写 NumberOnly 而是写 IsNumberKey .

谢谢...


It will work for you.

Note: Please change your naming conventions. Don''t write id as text1 instead write txtNumber. And for function don''t write NumberOnly instead write IsNumberKey.

Thanks...


这篇关于如何限制用户仅输入数字值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆