简体   繁体   English

如何限制输入元素中允许的字符集?

[英]How to restrict set of allowed characters in input element?

I know that this question has been asked before, but all solutions that I've seen here had some limitations. 我知道这个问题曾经被问过,但是我在这里看到的所有解决方案都有一定的局限性。

What I'm trying to achieve: I have an input field that should allow entering or pasting only alphanumeric characters, and total length must not exceed N characters. 我要实现的目标:我有一个输入字段,应只允许输入或粘贴字母数字字符,并且总长度不得超过N个字符。 It should work in mobile browsers. 它应该可以在移动浏览器中使用。

What I have tried so far: 到目前为止我尝试过的是:

  1. input event. 输入事件。 Triggered only when the focus is lost. 仅在失去焦点时触发。
  2. onkeypress event ( return this.value.length < 8 && (event.keyCode >= 65 && event.keyCode <= 90 || event.keyCode >= 97 && event.keyCode <= 122 || event.keyCode >= 48 && event.keyCode <= 57) ). onkeypress事件( return this.value.length < 8 && (event.keyCode >= 65 && event.keyCode <= 90 || event.keyCode >= 97 && event.keyCode <= 122 || event.keyCode >= 48 && event.keyCode <= 57) )。 Doesn't affect pasting and for some strange reason doesn't work at all in Chrome on Android. 不会影响粘贴,并且出于某些奇怪的原因在Android的Chrome浏览器中根本无法使用。
  3. pattern attribute. 模式属性。 Works fine, but doesn't validate anything "on the fly" - all validation happens when the form is submitted. 工作正常,但不“即时”验证任何内容-提交表单时进行所有验证。

As far as I know, it is a typical task (CVV codes / card numbers on payment forms, order numbers in e-commerce, etc.), but all solutions have some drawbacks. 据我所知,这是一项典型的任务(CVV代码/付款单上的卡号,电子商务中的订单号等),但是所有解决方案都有一些缺点。 Seems that I am missing something. 似乎我缺少了一些东西。 Please help. 请帮忙。

I once needed to do the same thing and I found that the best solution is to have a listener that is called every time the input field changes. 我曾经需要做同样的事情,但我发现最好的解决方案是在每次输入字段发生更改时都调用一个侦听器。 For this you should be able to use either the oninput html attribute or the corresponding input javascript event. 为此,您应该可以使用oninput html属性或相应的input javascript事件。

This demo from W3Schools demonstrates pretty well how it responds to input changes. W3Schools的这个演示很好地展示了它如何响应输入更改。 In particular, it does not only fire when focus is lost and it works when pasting text as well. 特别是,它不仅会在失去焦点时触发,而且在粘贴文本时也会起作用。 Naturally, inside the listener you can check that the current text in the input conforms to whatever you want. 当然,在侦听器内部,您可以检查输入中的当前文本是否符合您的要求。

What's even cooler with this solution is that you can even edit the text inside the input field from within the listener. 此解决方案甚至更酷的是,您甚至可以从侦听器内部在输入字段中编辑文本。 For this you need to add the listener to the input event in javascript using addEventListener(...) , then just use this.value = 'new text'; 为此,您需要使用addEventListener(...)将侦听器添加到javascript中的input事件中,然后只需使用this.value = 'new text'; .

What about using Input Pattern ? 使用输入模式怎么样?

<input type="text" pattern="[A-Za-z]{3}" title="Three letters">
<input type="text" pattern="[0-9]{3}" title="Three numbers">    
<input type="text" pattern="[A-Za-z0-9]{3}" title="Three alphanumeric">

combined with something like this answer and this 结合这样的答案这个

Using the validation built into Html5 inputs will prevent non-numeric input when you place the type=number attribute to an input. 当您将type = number属性放置到输入时,使用内置在Html5输入中的验证将防止非数字输入。 You could also use the max attribute if you want to set a maximum number to be allowed for entry. 如果要设置允许输入的最大数字,也可以使用max属性。 You still need to implement some type of client-side code to prevent numbers with older browsers, but the basic Html5 validation does a lot o work for you on any decent "real" browser (Chrome, Firefox, IE v9+, ect) 您仍然需要实现某种类型的客户端代码,以防止使用旧版浏览器出现数字,但是基本的Html5验证在任何不错的“真实”浏览器(Chrome,Firefox,IE v9 +等)上都能为您带来很多帮助

Here is documentation on the input type="number" 是有关输入type =“ number”的文档

  • elements automatically invalidate any entry that isn't a number (or empty, unless required is specified). 元素会自动使所有非数字条目无效(或为空,除非指定了必填项)。
  • You can use the required attribute to make an empty entry invalid, ie the input has to be filled in. 您可以使用required属性使空白条目无效,即必须填写输入。
  • You can use the step attribute to constrain valid values to a certain set of steps (eg multiples of 10). 您可以使用step属性将有效值约束为一组特定的步骤(例如10的倍数)。
  • You can use the min and max attributes to constrain valid values to lower and upper bounds. 您可以使用min和max属性将有效值限制在上下限。

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

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