Javascript限制文本输入字符
我想将文本框的输入字符限制为 [a-z0-9_-]
。但是,每当执行此操作时,退格键和箭头键等按钮就不起作用。我在这个网站和其他网站上发现了一些尝试,但它们要么不能在所有浏览器上正常工作,要么使用黑名单。例如,W3Schools 网站示例将号码列入黑名单。有没有一种方法可以使用白名单(上面的)并且仍然允许使用退格键、箭头、home、end 等键?或者我是否必须添加与我想要允许的按键匹配的所有按键代码?我做了类似的事情(为了简单起见,缩短了)。
编辑 - 添加代码
<input type="text" onkeypress="return checkInput();">
function checkInput(){
return /[a-z0-9_-]/gi.test(String.fromCharCode(window.event.keyCode));
}
I am wanting to restrict the input characters for a text box to [a-z0-9_-]
. However whenever if do this buttons like backspace and the arrow keys don't work. I have found some attempts on this website and others but either they don't work properly on all browsers or they use a black list. For example the W3Schools website example black lists numbers. Is there a way to use white list (the one above) and still allow keys like backspace, arrows, home, end etc? Or do I have to add everyone of the key codes that match the keys I want to allow? I do something like this (this is shortened for simplicity).
EDIT - Added code
<input type="text" onkeypress="return checkInput();">
function checkInput(){
return /[a-z0-9_-]/gi.test(String.fromCharCode(window.event.keyCode));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
只需将示例中的正则表达式更改为如下所示:
或者更好的是,避免使用双重否定:
然后您可以查找退格键等的键码并检查它们:
或者甚至将它们放入您的正则表达式中:
Just change the regex in the example to something like this:
Or better yet, avoid the double negative with:
Then you can look up the keycodes of backspace, etc. and check for them too:
Or even put them in your regex:
您尚未提供任何代码示例,因此很难在响应中具体说明,但作为一般策略,请尝试以下操作:不要尝试将输入时可以输入的字符列入白名单,而是验证每次击键后的文本框,以确保它仍然包含有效字符。如果没有,请删除最后输入的字符。
这种方法将允许使用退格键等特殊键,同时实现您真正想要的:文本框中的有效值。
You haven't provided any code samples, so it's hard to be specific in a response, but as a general strategy, try this: instead of trying to whitelist characters that can be input while they are being typed in, validate the contents of the text box after every key stroke to make sure that it still contains valid characters. If it doesn't, remove the last character entered.
This approach will allow special keys like backspace, etc., while at the same time achieve what it sounds like you are really after: a valid value in the text box.
是的,您可以限制字符的输入。例如,创建一个函数来检查正在发生的情况,如果一切正常则返回 true,否则返回 false:
一旦有了该函数,将其挂接到您的输入中(这是使用 jQuery):
您可以进行尽可能复杂的检查想要,例如这将允许美元货币价值:
Yes you can limit the input of characters. For example create a function that checks what is going on, return true if everything is OK and false if not:
once you have the function, hook it into you input (this is with jQuery):
You can make as complicated a check as you want, for example this will allow for USD money values:
您可以按您喜欢的任何键,只要您不让值包含任何内容
不在白名单中。
You can press any key you like, as long as you keep the value from including anything
not in the white-list.
将此代码添加到 onkeypress 事件中。
为了浏览器兼容性,您可以添加
var k = e.keyCode == 0 ? e.charCode : e.keyCode;
Add this code to onkeypress event.
For browser compatibility, You can add
var k = e.keyCode == 0 ? e.charCode : e.keyCode;