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

问题描述

大家好,

我的代码有问题.
我已经使用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()" >

推荐答案

<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>


int.TryParse(txtBox.Value, out tmpInt)



希望对您有帮助



Hope that helps


<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...


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

08-12 05:42