JavaScript 键码只允许数字和加号

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5535449/
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-08-23 17:42:24  来源:igfitidea点击:

JavaScript keycode allow number and plus symbol only

javascriptkeycode

提问by cyberfly

I have this JavaScript function that is used to force user only type number in the textbox. Right now and I want to modify this function so it will allow the user to enter plus (+) symbol. How to achieve this?

我有这个 JavaScript 函数,用于强制用户仅在文本框中键入数字。现在,我想修改这个函数,以便它允许用户输入加号 (+) 符号。如何实现这一目标?

//To only enable digit in the user input

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

回答by dominicbri7

Since the '+' symbol's decimal ASCII code is 43, you can add it to your condition.

由于“+”符号的十进制 ASCII 代码是 43,您可以将其添加到您的条件中。

for example :

例如 :

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

This way, the Plus symbol is allowed.

这样,加号是允许的。

回答by Blender

This code might work. I added support for SHIFT + (equal sign)and the numpad +.

此代码可能有效。我添加了对SHIFT + (equal sign)和 numpad 的支持+

function isNumberKey(evt)
{
  var charCode = (evt.which) ? evt.which : event.keyCode;
  var shiftPressed = (window.Event) ? e.modifiers & Event.SHIFT_MASK : e.shiftKey;

  if ((shiftPressed && charCode == 187) || (charCode == 107))
  {
    return true;
  } else if ((charCode > 95) && (charCode < 106)) {
    return true;
  } else if (charCode > 31 && (charCode < 48 || charCode > 57))) {
    return false;
  } else {
    return true;
  }
}

回答by Santosh Linkha

this is stupid ... not really an answer at all. I would suggest you to do following.

这很愚蠢......根本不是真正的答案。我建议你做以下。

function isNumberKey(evt)
{
    console.log(evt.keyCode);
    return false;
}

And find out the ranges of all keys, and implement it.

并找出所有键的范围,并实现它。

回答by manoj

It's Work. Javascript keycode allow number and plus symbol only

这是工作。Javascript 键码只允许数字和加号

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JavaScript form validation</title>
</head>
<body>

<form name="form1" action="#">

Mobile Number:&nbsp;&nbsp;<input type='text'  id='PhoneNumber' maxlength="10" onKeyPress="return IsNumeric3(event);" ondrop="return false;" onpaste="return false;"/>
<span id="error3" style="color: Red; display: none">* Input digits (0 - 9)</span>

</form>
 <script type="text/javascript">
        var specialKeys = new Array();
        specialKeys.push(8); 
  specialKeys.push(43); 
  specialKeys.push(37); 
  specialKeys.push(39); 
  //Backspace
        function IsNumeric3(e) {
            var keyCode = e.which ? e.which : e.keyCode
            var ret = (keyCode != 37 && keyCode != 8 && keyCode != 46 && (keyCode >= 48 && keyCode <= 57) || specialKeys.indexOf(keyCode) != -1);
            document.getElementById("error3").style.display = ret ? "none" : "inline";
            return ret;
        }
    </script>

 <script>
function stringlength(inputtxt, minlength, maxlength)
{ 
var field = inputtxt.value; 
var mnlen = minlength;
var mxlen = maxlength;

if(field.length<mnlen || field.length> mxlen)
{ 
alert("Please input the 10 digit mobile number");
return false;
}
else
{ 

return true;
}
}
</script>
</body>
</html>

Thank you friends

谢谢各位朋友

回答by Shamim Hafiz

Here is the modified code:

这是修改后的代码:

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

回答by Infoconic

Here is the code . working fine with numbers and plus + sign in phone fields. you will have to implement the code on keydown function . target the id/class of the particular phone field and use keydown function.

这是代码。在电话字段中使用数字和加号 + 符号工作正常。你将不得不在 keydown 函数上实现代码。定位特定电话字段的 id/class 并使用 keydown 功能。

    //allows only these keys
    // backspace, delete, tab, escape, and enter
    if ( event.keyCode == 107 || event.keyCode == 46 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 27 || event.keyCode == 13 || 
         // Ctrl+A
        (event.keyCode == 65 && event.ctrlKey === true) || 
         // home, end, left, right
        (event.keyCode >= 35 && event.keyCode <= 39)) {
             return;
    }
    else {
        // Ensure that it is a number and stop the keypress
        if (event.shiftKey || (event.keyCode < 48 || event.keyCode > 57) && (event.keyCode < 96 || event.keyCode > 105 )) {
            event.preventDefault(); 
        }   
    }

回答by chak_zefir

Using experience of my colleagues above, write one function, that fits me well. It filters all except numbers, arrows and backspace. Maybe it would be useful for somebody.

利用上面同事的经验,写一个函数,很适合我。它过滤除数字、箭头和退格之外的所有内容。也许这对某人有用。

function isKeyCorrect(keyEvent) {
    var charCode = keyEvent.which ? keyEvent.which : keyEvent.keyCode;
    var isNotNumber = charCode < 48 || charCode > 57;
    var isNotArrow = charCode < 37 || charCode > 40;
    var isNotBackspace = charCode !== 8;

    return isNotNumber && isNotArrow && isNotBackspace;
}

回答by Adem O?lu

<script type="text/javascript">
 $(document).ready(function() {
    `enter code here`  $('#form-1').submit(function(msg) {  
        $.post("action.php?act=login",$(this).serialize(),function(data){      
            if (data == 'ERR:A3001004') { alert("Güvenlik hatas?, sayfay? yenileyin."); }
            else if (data == 'TIMEEND') { alert("Anahtar?n?z?n süresi dolmu?.");    }
            else if (data == 'INVALID') { alert("Ge?ersiz anahtar ?ifresi girdiniz.");  }
            else if (data == 'OK') { alert("Ba?ar?yla giri? yapt?n?z. Yeti?kinlere g?re i?erik bar?nd?ran sitelere eri?im sa?layabilirsiniz."); }
        });

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

         return true;
      }
</script>