简体   繁体   English

用Java语言进行简单的数字输入验证(Firefox错误)

[英]Simple number input validation in Javascript (Firefox error)

function onlyNumberInput(evt) {
var theEvent = evt || window.event;
  var key = theEvent.keyCode || theEvent.which;
  key = String.fromCharCode( key );
  var regex = /[0-9]/;
  if( !regex.test(key) ) {
    theEvent.returnValue = false;
    if(theEvent.preventDefault) theEvent.preventDefault();
  }
}

Works fine on Chrome but in Firefox I cant press delete or backspace key to edit number. 在Chrome上工作正常,但在Firefox中我无法按Delete或Backspace键来编辑号码。 Where is the problem in my script? 我的脚本中的问题出在哪里? Of course I can use another way to do this but i want to know what errors is going on Firefox to execute above script? 当然,我可以使用另一种方法来执行此操作,但是我想知道Firefox在执行上述脚本时发生了什么错误?

if(theEvent.preventDefault) theEvent.preventDefault();

You missed " { " 您错过了“ {

if(theEvent.preventDefault){
  theEvent.preventDefault();
}

JavaScript is CaseSensitive. JavaScript是区分大小写的。 Remember that. 记住这一点。 Hope it helped. 希望能有所帮助。 Good luck. 祝好运。

Hello @Raju Ahmed i check your code and figure that u are testing key's with regex but the problem is it's only checking for number's and this is what u asked. 您好@Raju Ahmed我检查了您的代码,并确定您正在使用regex测试密钥,但是问题是,它仅检查数字,这就是您的要求。 But also u need to check for Delete Key(which is 46) and also check for Backspace(which is 8) and other keys what u want to use. 但是,您还需要检查Delete Key(它是46)并检查Backspace(它是8)和其他您要使用的键。

Also i test your code on Chrome build 55.0.2883.87 m (64-bit) and it's also not working for delete, backspace and others. 另外,我还在Chrome版本55.0.2883.87 m(64位)上测试了您的代码,它也不适用于删除,退格和其他功能。

You might want to use like this if u use jQuery: 如果您使用jQuery,则可能需要这样使用:

function onlyNumberInput(evt) {
        var theEvent = evt || window.event;
        var allowed = [8, 17, 46, 37, 38, 39, 40];
        var key = theEvent.keyCode || theEvent.which;
        var regex = /[0-9]/;
        if($.inArray(allowed, key) !== -1) {
            key = String.fromCharCode( key );
            if( !regex.test(key) ) {
                theEvent.returnValue = false;
                if(theEvent.preventDefault) 
                    theEvent.preventDefault();
            }
        }
    }

If you want to use other keys, find the keyCode the key and add into allowed. 如果要使用其他键,请找到该键的keyCode并添加到允许的键中。 If you don't use JQuery in your code please let me know. 如果您未在代码中使用JQuery,请告诉我。 I can change it to normal version :) 我可以将其更改为普通版本:)

One more thing this is allowed to use in JS if you have only one line job: 如果您只有一项工作,则可以在JS中使用另一件事:

if(theEvent.preventDefault) 
    theEvent.preventDefault();

Edit II: 编辑二:

Well @Raju i have check the code again it's not working as u wish. 好吧,@ Raju我已经再次检查了代码,它没有按您希望的那样工作。 So i found an example about it with a plugin if you want to use it. 因此,如果您想使用它,我找到了一个带有插件的示例。

Plugin : NumericInput, 插件:NumericInput,

Demo: Demo 演示: 演示

You can check this link about number only input's: HTML Text Input allow only Numeric input 您可以检查有关仅数字输入的链接: HTML文本输入仅允许数字输入

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

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