JavaScript键代码允许数字和加上符号

发布于 2024-10-29 08:38:02 字数 351 浏览 4 评论 0原文

我有一个 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;
}

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?

//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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

玩世 2024-11-05 08:38:02

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

例如:

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

这样,就允许使用加号。

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

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.

羁〃客ぐ 2024-11-05 08:38:02

这段代码可能有效。我添加了对 SHIFT +(等号) 和小键盘 + 的支持。

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

This code might work. I added support for SHIFT + (equal sign) and the 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;
  }
}
汐鸠 2024-11-05 08:38:02

这很愚蠢……根本不是一个真正的答案。我建议你做以下事情。

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

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

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.

微暖i 2024-11-05 08:38:02

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

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

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

Mobile Number:  <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>

谢谢朋友们

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

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

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

Mobile Number:  <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

无语# 2024-11-05 08:38:02

这是修改后的代码:

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

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;
}
人事已非 2024-11-05 08:38:02

这是代码。在电话字段中可以很好地处理数字和加号。您必须在 keydown 函数上实现代码。定位特定电话字段的 ID/类别并使用 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(); 
        }   
    }

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.

    //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(); 
        }   
    }
爱格式化 2024-11-05 08:38:02

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

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

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;
}
贱贱哒 2024-11-05 08:38:02
<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>
<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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文