如何防止将无效字符输入到输入字段中

Mik*_*ike 12 javascript regex javascript-events

Onkeydown,我运行以下JavaScript:

function ThisOnKeyDown(el) {
   if (el.title == 'textonly') {
       !(/^[A-Za-zÑñ-\s]*$/i).test(el.value) ? el.value = el.value.replace(/[^A-Za-zÑñ-\s]/ig, '') : null;
   }
   if (el.title == 'numbersonly') {
       !(/^[0-9]*$/i).test(el.value) ? el.value = el.value.replace(/[^0-9]/ig, '') : null;
   }
   if (el.title == 'textandnumbers') {
       !(/^[A-Za-zÑñ0-9-\s]*$/i).test(el.value) ? el.value = el.value.replace(/[^A-Za-zÑñ0-9-\s]/ig, '') : null;
   }
}
Run Code Online (Sandbox Code Playgroud)

这三个标题属性之一被赋予页面上的各种输入字段.只要无效字符被正确删除,代码就会起作用,但直到输入下一个字符为止.我想找到一种简单地拒绝无效输入的方法.我感谢您的帮助!

编辑:我全局创建事件.我是这样做的:

      function Globalization() {
      var inputs = document.getElementsByTagName('input');
      for (i = 0; i < inputs.length; i++) {
          inputs[i].onfocus = createEventHandler(
              ThisOnFocus, inputs[i]);
          inputs[i].onblur = createEventHandler(
              ThisOnBlur, inputs[i]);
          inputs[i].onkeydown = createEventHandler(
              ThisOnKeyDown, inputs[i]);
          inputs[i].onkeyup = createEventHandler(
              ThisOnKeyUp, inputs[i]);
      }
  }
Run Code Online (Sandbox Code Playgroud)

Globalization() 运行 body.onload

因此,典型的输入字段具有不带函数调用的HTML,如下所示:

          <input id="AppFirstName" style="width: 150px;" type="text" maxlength="30" title="textonly"/>
Run Code Online (Sandbox Code Playgroud)

use*_*772 21

要防止它首先被设置,您可以在keydown事件处理程序上返回false,从而防止事件进一步传播.

我使用jQuery编写了下面的示例,但是在传统绑定时可以使用相同的函数.

虽然在服务器端验证也很重要,但客户端验证对于用户友好性而言非常重要.

$("input.number-only").bind({
    keydown: function(e) {
        if (e.shiftKey === true ) {
            if (e.which == 9) {
                return true;
            }
            return false;
        }
        if (e.which > 57) {
            return false;
        }
        if (e.which==32) {
            return false;
        }
        return true;
    }
});
Run Code Online (Sandbox Code Playgroud)

  • 'which' 属性现在已弃用,使用 'keyCode' 代替:https://developer.mozilla.org/en-US/docs/Web/Events/keydown (3认同)
  • 作为参考,键码数字(9、57、32 等)代表 unicode 字符,列于此处:https://en.wikipedia.org/wiki/List_of_Unicode_characters (2认同)

小智 6

上面的代码确实说了-仅允许数字。您可以通过添加例外来修改它,例如说BACKSPACE

<html>
    <head>
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <script>
            function keyispressed(e){
                var charval= String.fromCharCode(e.keyCode);
                if((isNaN(charValue)) && (e.which != 8 )){ // BSP KB code is 8
                    e.preventDefault();
                }
                return true;
            }
        </script>
    </head>

    <body>
        <input type="text" onkeydown="return keyispressed(event);"/>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)


Dus*_*ler 5

您可以轻松地在单个 html 行中完成此操作。

像这样禁止空格:

<input type="text" onkeypress="return event.charCode != 32">
Run Code Online (Sandbox Code Playgroud)

或者只允许数字:

<input type="text" onkeypress='return event.charCode >= 48 && event.charCode <= 57'>
Run Code Online (Sandbox Code Playgroud)

只需查找您想要允许或禁止的任何号码的unicode即可。


Ran*_*Dev 5

您可以在“输入”事件期间替换您的输入值:

// <input oninput=onInput(event) />

const onInput = event => {
    event.target.value = event.target.value.replace(/[^0-9+]/g, '')
}
Run Code Online (Sandbox Code Playgroud)

来源: https: //knplabs.com/en/blog/how2-tips-how-to-restrict-allowed-characters-inside-a-text-input-in-one-line-of-code