javascript 表单域中只允许数字或小数点

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17583685/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 08:53:06  来源:igfitidea点击:

Allow only numbers or decimal point in form field

javascriptforms

提问by SirBdon

I've limited the input field to only numbers through js but am not sure how to also allow decimals...

我通过 js 将输入字段限制为仅数字,但不确定如何也允许小数...

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

         return true;
      }

Thank you in advance!

先感谢您!

Answer:

回答:

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

 return true;
 }

Adding the charCode 46 worked perfectly (keypress value). 190 and 110 did nothing.

添加 charCode 46 效果很好(按键值)。190和110什么也没做。

Thanks for your help all!

感谢大家的帮助!

回答by Paul S.

Codes depend on which event you're listening to, an easy way to find what you want is by using the JavaScriptEvent KeyCode Test Page herewith test input, e.g. for a full stop (the .left of right shift) you have

代码取决于您正在收听的事件,找到您想要的内容的一种简单方法是在此处使用带有测试输入的JavaScript事件 KeyCode 测试页面,例如,对于您拥有的句号(右移的左侧).

               onKeyDown    onKeyPress    onKeyUp
event.keyCode        190            46        190
event.charCode         0            46          0
event.which          190            46        190

and for the .on the numeric pad it is

对于.数字键盘上的

               onKeyDown    onKeyPress    onKeyUp
event.keyCode        110            46        110
event.charCode         0            46          0
event.which          110            46        110

As you can see, it is most uniform to check with onKeyPresswith charCodewhich is it's unicode number; String.fromCharCode(46); // ".".

如您所见,使用charCode来检查onKeyPress是最统一的,这是它的 unicode 编号;.String.fromCharCode(46); // "."

There is a full list on the MDN pagefor KeyboardEventwhere it is also stated

在MDN页面有一个完整KeyboardEvent列表,其中也有说明

Note: Web developers shouldn't use keycode attribute of printable keys in keydown and keyup event handlers. As described above, keycode is not usable for checking character which will be inputted especially when Shift key or AltGr key is pressed. When web developers implement shortcut key handler, keypress event is better event for that purpose on Gecko at least. See Gecko Keypress Eventfor the detail.

注意:Web 开发人员不应在 keydown 和 keyup 事件处理程序中使用可打印键的 keycode 属性。如上所述,键码不能用于检查将输入的字符,尤其是在按下 Shift 键或 AltGr 键时。当 Web 开发人员实现快捷键处理程序时,至少在 Gecko 上,keypress 事件是更好的事件。有关详细信息,请参阅Gecko 按键事件

You can observe the strange effects of using AltGror Shifton keyCodewith the key of choice in the test page I linked to as well.

您也可以在我链接到的测试页面中观察在keyCode上使用AltGrShift以及选择的键的奇怪效果。

回答by fjdumont

With HTML5, the inputelement got a new attribute: pattern. You can specify a regular expression that is validated by the browser before submitting the form.

在 HTML5 中,input元素获得了一个新属性:pattern. 您可以在提交表单之前指定由浏览器验证的正则表达式。

<input type="text" pattern="([0-9]+\.)?[0-9]+" />

It is not widely supported yet, though.

不过,它还没有得到广泛支持

The pattern attribute is supported in Internet Explorer 10, Firefox, Opera, and Chrome. (w3schools)

Internet Explorer 10、Firefox、Opera 和 Chrome 支持模式属性。 ( w3schools)

Disclaimer: the expression is weak, go build a better one :)

免责声明:表达很弱,去建立一个更好的:)

回答by Alexander Mistakidis

Assuming you're using on key down or up:

假设您使用的是按下或按下键:

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

     return true;
  }

110 is decimal point, 190 is period

110是小数点,190是句号

http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

http://www.cambiaresearch.com/articles/15/javascript-char-codes-key-codes

Heres a fiddle with some test cases

这是一些测试用例的小提琴