简体   繁体   English

防止单词之间超过一个空格

[英]Prevent more than one space between words

I have a function that prevents people putting numbers or any symbol but letters onkeypress into a text box due to ongoing problems with data entry.由于数据输入的持续问题,我有一个功能可以防止人们将数字或任何符号而不是字母 onkeypress 放入文本框中。

<td><input type="text" name="name" onkeypress="return isAlfa(event)"></td>

Now some staff for reasons unknown put two spaces between words at random times.现在一些工作人员出于未知原因在单词之间随机放置了两个空格。 So I need to prevent them putting more than one space between words.所以我需要防止他们在单词之间放置一个以上的空格。 I want to do this in the same function, but it keeps breaking.我想在同一个函数中执行此操作,但它一直在中断。

function isAlfa(evt) {
  evt = (evt || window.event);
  var charCode = (evt.which || evt.keyCode);

  if ((charCode > 32)
    && (charCode < 65 || charCode > 90)
    && (charCode < 97 || charCode > 122)
  ) {
    return false;
  }
  return true;
}

How can I prevent them entering more than one space between words?如何防止他们在单词之间输入多个空格?

Neglecting all the other helpful suggestions and comments and strictly following the OP's requirements one has to ...忽略所有其他有用的建议和评论,并严格遵循 OP 的要求,必须...

  1. Adapt the return condition in a way that takes into account if, with the current keystroke, a whitespace sequence is going to be created.以某种方式调整返回条件,以考虑是否使用当前击键创建空白序列。
  2. Thus one has to implement a method that determines exactly that.因此,必须实现一种方法来准确确定这一点。
  3. There might be some possible helper methods too.也可能有一些可能的辅助方法。
  4. code example ...代码示例...

 function isWhiteSpace(char) { return (/\\s/).test(char); } function willCreateWhitespaceSequence(evt) { var willCreateWSS = false; if (isWhiteSpace(evt.key)) { var elmInput = evt.currentTarget; var content = elmInput.value; var posStart = elmInput.selectionStart; var posEnd = elmInput.selectionEnd; willCreateWSS = ( isWhiteSpace(content[posStart - 1] || '') || isWhiteSpace(content[posEnd] || '') ); } return willCreateWSS; } function isAlfa(evt) { evt = (evt || window.event); var charCode = (evt.which || evt.keyCode); return (( (charCode > 32) && (charCode < 65 || charCode > 90) && (charCode < 97 || charCode > 122) ) || willCreateWhitespaceSequence(evt)) ? false : true; }
 <input type="text" name="name" onkeypress="return isAlfa(event)"/>

This code will prevent multiple white spaces, that is, it will only allow 1 space and it will prevent two or more white spaces.此代码将防止出现多个空格,也就是说,它只允许 1 个空格,并且将防止出现两个或更多空格。 You can configure the amount of white space you want to deny.您可以配置要拒绝的空白空间量。

    
        

Rather than listening for and evaluating each key press it would be far simpler just to react to any and all input and then sanitise whatever was entered via a RegExp.与其监听和评估每个按键,不如对任何和所有输入做出反应,然后清理通过 RegExp 输入的任何内容。

Example:例子:

<input type=text id=myfield />

JS: JS:

document.querySelector('#myfield').addEventListener('input', evt => {
    evt.target.value = evt.target.value.replace(/[^a-z\s]/ig, '').replace(/\s{2,}/g, ' ');
});

That first replaces all non-letters and spaces with nothing, then replaces sequences of 2 or more spaces with a single space.首先用空替换所有非字母和空格,然后用一个空格替换 2 个或更多空格的序列。

may be try to use something like this.可能会尝试使用这样的东西。 on your keypress event try to check the last value by using string substr method and if that gives you a space and current code is also a Space then prevent or do whatever you want to do with input.在您的keypress事件中,尝试使用string substr方法检查最后一个值,如果这给了您一个空格并且当前代码也是一个空格,则阻止或执行您想要对输入执行的任何操作。

 function checkstuff(event){ if (event.target.value.substr(-1) === ' ' && event.code === 'Space') { console.log('space pressed in sequence'); } }
 <input type='text' onkeypress="checkstuff(event)">

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

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