简体   繁体   English

如何使用普通 javascript 防止输入字段中出现空格

[英]How prevent whitespace in input field with plain javascript

I have an username input field and trying to prevent user fill them with white spaces.我有一个用户名输入字段,并试图防止用户用空格填充它们。

<input type="text" name="username" />

i do this and whitespace isn't blocked我这样做并且空白没有被阻止

var
  field = document.querySelector('[name="username"]');

field.addEventListener('keypress', function ( event ) {  
   var 
     key = event.keyCode;

   return (key !== 32);
});

Use event.preventDefault to prevent its default behavior.使用event.preventDefault来防止其默认行为。

 var field = document.querySelector('[name="username"]'); field.addEventListener('keypress', function ( event ) { var key = event.keyCode; if (key === 32) { event.preventDefault(); } });
 <input type="text" name="username" />

If you want to use the return false;如果你想使用return false; , then you should use the onkeypress of the input instead, jsfiddle ,那么你应该使用输入的onkeypressjsfiddle

field.onkeypress = function(e) {
   var  key = e.keyCode;
   return (key !== 32);
};

Modify the input like:修改输入,如:

<input type="text" name="username" onkeypress="CheckSpace(event)"/>

Then the javascript is:然后javascript是:

<script type="text/javascript">

function CheckSpace(event)
{
   if(event.which ==32)
   {
      event.preventDefault();
      return false;
   }
}

Try checking for all the different kinds of whitespaces listed here Are there other whitespace codes like &nbsp for half-spaces, em-spaces, en-spaces etc useful in HTML?尝试检查此处列出的所有不同类型的空格在 HTML 中是否还有其他空格代码,例如 &nbsp 用于半空格、em-spaces、en-spaces 等?

So you code would be:所以你的代码是:

var field = document.querySelector('[name="username"]');

field.addEventListener('keypress', function ( event ) {  
   var 
   key = event.keyCode;

   return (key !== 32 && key !== 160 && key != 5760 && key != 8192 && key != 8192 && key != 8194 && key != 8195 && key != 8196 && key != 8197 && key != 8198 && key != 8199 && key != 8200 && key != 8201 && key != 8202 && key != 8232 && key != 8233 && key != 8239 && key != 8287 && key != 12288);
});

Here is the complete list of all the different kinds of whitespaces: https://en.wikipedia.org/wiki/Whitespace_character#Spaces_in_Unicode以下是所有不同类型空格的完整列表: https ://en.wikipedia.org/wiki/Whitespace_character#Spaces_in_Unicode

In case anyone needs this to be done which will replace all whitespace automatically and will not allow user to put space and will force them to put username without space even then copy paste .如果有人需要这样做,它将自动替换所有空格,并且不允许用户放置空间,并且会强制他们放置 username 没有空格,即使然后复制粘贴。 Here is the code.这是代码。

<script type="text/javascript">
var field = document.querySelector('[name="username"]');

field.addEventListener('keyup', function ( event ) {  
   var userName = field.value;
  userName = userName.replace(/\s/g, '');
  field.value = userName;
});

</script>

HTML only solution using the pattern attribute and regex.仅使用pattern属性和正则表达式的 HTML 解决方案。

 <form> <input type="text" name="username" pattern="[^\s]+"> <button type="submit">Submit</button> </form>

        const key = e.keyCode
        const keyCodes = [
          32, 160, 5760, 8192, 8192, 8194, 8195, 8196, 8197, 8198, 8199,
          8200, 8201, 8202, 8232, 8233, 8239, 8287, 12288,
        ]
        if (keyCodes.some((val) => val === key)) {
          e.preventDefault()
        }

here is a simple solution !这是一个简单的解决方案!

Hey i have simple solution regarding your question try one嘿,关于你的问题,我有一个简单的解决方案试试

If you want to submit only text and whitespace than use this one如果你只想提交文本和空格而不是使用这个

<input type="text" name="Name" required pattern="[a-zA-Z ]+" >

If you want to submit number and whitespace than use this one如果你想提交数字和空格而不是使用这个

<input type="text" name="Name" required pattern="[0-9 ]+" >

If you want to insert text not whitespace than use this one如果你想插入文本而不是空格而不是使用这个

<input type="text" name="Name" required pattern="[a-zA-Z]+" >

Use any line according to your requirements no extra line of code or condition simple and secure根据您的要求使用任何行没有额外的代码行或条件简单安全

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

相关问题 如何防止在密码输入字段中输入空格/空格 | ReactJs - How to prevent to enter space/whitespace in password input field | ReactJs 如何使用 JavaScript 检查输入字段是否填充了空格? - How do you check if an input field is filled with whitespace using JavaScript? 如何使用JavaScript阻止文本字段中的任何类型的输入? - How to prevent any type of input in text field using JavaScript? 如何使用javascript防止输入字段中的空格和句号 - How to prevent spaces and full stops in input field with javascript Javascript / jQuery:如何防止活动输入字段被更改? - Javascript / jQuery: How to prevent active input field from being changed? javascript如何防止输入字段中出现多个点 - javascript how to prevent from mulitiple dots in input field 如何使用普通 javascript 创建输入范围 - How to create a input range using plain javascript 从输入字段验证onchange文件名(纯JavaScript) - Validate onchange filename from input field (plain javascript) jquery / javascript将纯文本消息转换为文本输入字段 - jquery/javascript convert a plain text message into a text input field 如何防止带空格的JavaScript损坏WordPress? - How do I prevent JavaScript with whitespace from breaking in WordPress?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM