防止单词之间超过一个空格 [英] 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.
<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?
推荐答案
可以尝试使用这样的东西.在您的 keypress
事件上尝试使用 string
substr
方法检查最后一个值,如果这给了您一个空格并且当前代码也是一个空格然后阻止或做任何你想对输入做的事情.
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.
function checkstuff(event){
if (event.target.value.substr(-1) === ' ' && event.code === 'Space') {
console.log('space pressed in sequence');
}
}
<input type='text' onkeypress="checkstuff(event)">
这篇关于防止单词之间超过一个空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!