只允许表单字段中的数字或小数点 [英] Allow only numbers or decimal point in form field

查看:123
本文介绍了只允许表单字段中的数字或小数点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经通过js将输入字段限制为只有数字,但不知道如何还允许小数...

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

返回true;
}

预先感谢您!

答案:

 函数isNumberKey(evt)
{
var charCode =(evt。哪一个) ? (charCode> 31&&(charCode< 48 || charCode> 57)&&& charCode!= 46)
return false; event.keyCode
if

返回true;



$ b

添加charCode 46完美工作(按键值)。 190和110什么都没做。



感谢您的全力帮助!

解决方案

代码取决于您正在侦听的事件,通过使用 JavaScript 事件KeyCode测试页面 here 与测试输入,例如为了完全停止(左移右移),您有

  onKeyDown onKeyPress onKeyUp 
event.keyCode 190 46 190
event.charCode 0 46 0
event.which 190 46 190
,它是



  onKeyDown onKeyPress onKeyUp 
event.keyCode 110 46 110
event.charCode 0 46 0
event.which 110 46 110
charCode onKeyPress 是最为一致的。 >这是unicode号码; 使用String.fromCharCode(46); //。



有一个 MDN页面上的完整列表,用于 KeyboardEvent


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


您可以观察在 keyCode 上使用 AltGr Shift 我也链接到测试页面。


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

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.

Thanks for your help all!

解决方案

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

               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 onKeyPress with charCode which is it's unicode number; String.fromCharCode(46); // ".".

There is a full list on the MDN page for KeyboardEvent where it is also stated

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 Event for the detail.

You can observe the strange effects of using AltGr or Shift on keyCode with the key of choice in the test page I linked to as well.

这篇关于只允许表单字段中的数字或小数点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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