如何使用 jquery 阻止或限制输入字段中的特殊字符?
如何使用 jquery 阻止在输入字段中输入特殊字符?
How do I block special characters from being typed into an input field with jquery?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(27)
一个使用正则表达式的简单示例,您可以更改它以允许/禁止您喜欢的任何内容。
A simple example using a regular expression which you could change to allow/disallow whatever you like.
我正在寻找一个答案,将输入限制为仅字母数字字符,但仍然允许使用控制字符(例如,退格键、删除键、制表符)和复制+粘贴。 我尝试提供的答案都不满足所有这些要求,因此我使用
input
事件提出了以下内容。编辑:
作为 rinogo 在注释中指出,上面的代码片段在输入文本中间键入时强制光标移动到输入的末尾。 我相信下面的代码片段可以解决这个问题。
I was looking for an answer that restricted input to only alphanumeric characters, but still allowed for the use of control characters (e.g., backspace, delete, tab) and copy+paste. None of the provided answers that I tried satisfied all of these requirements, so I came up with the following using the
input
event.Edit:
As rinogo pointed out in the comments, the above code snippet forces the cursor to the end of the input when typing in the middle of the input text. I believe the code snippet below solves this problem.
简短回答:防止“按键”事件:
长回答:使用像 jquery.alphanum
选择解决方案时需要考虑以下几点:
我认为这个领域足够复杂,足以保证使用第 3 方插件。 我尝试了几个可用的插件,但发现每个插件都有一些问题,所以我继续编写 jquery.alphanum 。 代码如下所示:
或者为了更细粒度的控制,添加一些设置:
希望它有帮助。
Short answer: prevent the 'keypress' event:
Long answer: Use a plugin like jquery.alphanum
There are several things to consider when picking a solution:
I think this area is complex enough to warrant using a 3rd party plugin. I tried out several of the available plugins but found some problems with each of them so I went ahead and wrote jquery.alphanum. The code looks like this:
Or for more fine-grained control, add some settings:
Hope it helps.
使用简单的内联 onkeypress 事件。
Use simple onkeypress event inline.
使用HTML5的模式输入属性!
Use HTML5's pattern input attribute!
使用正则表达式允许/禁止任何内容。 此外,对于比接受的答案稍微更强大的版本,可以通过首先传递按键事件并允许没有与其关联的键值的字符(退格键、制表符、箭头键、删除等)来完成根据键码而不是值检查键。
Use regex to allow/disallow anything. Also, for a slightly more robust version than the accepted answer, allowing characters that don't have a key value associated with them (backspace, tab, arrow keys, delete, etc.) can be done by first passing through the keypress event and check the key based on keycode instead of value.
您的文本框:
您的 JavaScript:
Your textbox:
Your javascript:
看一下 jQuery 字母数字插件。 https://github.com/KevinSheedy/jquery.alphanum
Take a look at the jQuery alphanumeric plugin. https://github.com/KevinSheedy/jquery.alphanum
这是一个阻止用户
在此处键入字符“a”键代码的示例:
http://www.expandinghead.net/keycode.html
this is an example that prevent the user from typing the character "a"
key codes refrence here:
http://www.expandinghead.net/keycode.html
我使用这段代码修改我看到的其他代码。 仅当按下的按键或粘贴的文本通过模式测试(匹配)时,才对用户写入(此示例是仅允许 8 位数字的文本输入)
I use this code modifying others that I saw. Only grand to the user write if the key pressed or pasted text pass the pattern test (match) (this example is a text input that only allows 8 digits)
在文本框的 onkeypress 事件上编写一些 JavaScript 代码。
根据要求允许和限制文本框中的字符
Write some javascript code on onkeypress event of textbox.
as per requirement allow and restrict character in your textbox
替换特殊字符、空格并转换为小写
To replace special characters, space and convert to lower case
您可以使用纯 JavaScript 来实现此操作,您可以将其放在
onKeyUp
事件中。限制 - 特殊字符
接受 - 仅数字
接受 - 仅小字母
我可以为更多场景编写,但我必须保留具体答案。
注意它将与 jquery、react、Angular 等一起使用。
You can achieve this using plain JavaScript, You can put this in the
onKeyUp
event.Restrict - Special Characters
Accept - Number only
Accept - Small Alphabet only
I could write for some more scenarios but I have to maintain the specific answer.
Note It will work with jquery, react, angular, and so on.
是的,您可以使用 jQuery 作为:
并且 user_availability.php 的脚本将是:
我尝试添加 / 和 \ 但不是成功了。
您还可以使用 javascript & 来完成此操作。 代码将是:
Yes you can do by using jQuery as:
and script for your user_availability.php will be:
I tried to add for / and \ but not succeeded.
You can also do it by using javascript & code will be:
想评论亚历克斯对戴尔答案的评论。 不可能(首先需要多少“代表”?这不会很快发生......奇怪的系统。)
因此,作为答案:
可以通过将 \b 添加到正则表达式定义来添加退格键,如下所示:[a-zA-Z0-9\b]。
或者您只需允许整个拉丁语范围,包括或多或少的任何“非外来”字符(也控制像退格键这样的字符): ^[\u0000-\u024F\u20AC]+$
只有拉丁语之外的真正的unicode字符有欧元符号(20ac),添加您可能需要的其他内容。
要处理通过复制和粘贴输入的输入,只需绑定到“更改”事件并检查那里的输入 - 删除它或剥离它/给出诸如“不支持的字符”之类的错误消息。
Wanted to comment on Alex's comment to Dale's answer. Not possible (first need how much "rep"? That wont happen very soon.. strange system.)
So as an answer:
Backspace can be added by adding \b to the regex definition like this: [a-zA-Z0-9\b].
Or you simply allow the whole Latin range, including more or less anything "non exotic" characters (also control chars like backspace): ^[\u0000-\u024F\u20AC]+$
Only real unicode char outside latin there is the euro sign (20ac), add whatever you may need else.
To also handle input entered via copy&paste, simply also bind to the "change" event and check the input there too - deleting it or striping it / giving an error message like "not supported characters"..
限制按键上的特殊字符。 这是关键代码的测试页: http://www.asquare.net/javascript/tests /KeyCode.html
在 Angular 中,我需要在文本字段中使用正确的货币格式。 我的解决方案:
在 html 中添加指令
,并在相应的角度控制器中我只允许只有 1 个句点,将文本转换为数字并在“模糊”上添加数字舍入
Restrict specials characters on keypress. Here's a test page for key codes: http://www.asquare.net/javascript/tests/KeyCode.html
In Angular, I needed a proper currency format in my textfield. My solution:
In the html add the directive
and in the corresponding angular controller I only allow there to be only 1 period, convert text to number and add number rounding on 'blur'
仅数字:
对于包括
的时间:
将57
替换为58
。 还包括删除和退格:Just the numbers:
For time including
:
replace57
with58
. To also include delete and backspace:最简单方法。
这是使用
indexOf
验证输入是否以开头的
This is the simplest way
indexOf
is used to validate if the input started with.
在 HTML 中:
在 JS 中:
在此示例中可以输入重音符号。
In HTML:
In JS:
In this example it is possible to type accents.
文本框中仅允许数字(限制字母和特殊字符)
Allow only numbers in TextBox (Restrict Alphabets and Special Characters)
使用下面的代码也可以限制特殊字符
Use below code to also restrict special characters
尝试这个 JavaScript 代码,这是限制输入中特殊字符的简单方法。
源代码:限制特殊字符
Try this JavaScript Code it's a simple way to restrict special characters from the input.
Source code: Restrict special characters
这是我的一句话方法......
它可以处理按键、复制粘贴和点击自动完成建议。 特殊字符会短暂出现,但会立即消失。
Here's my one-liner approach...
It can handle key presses, copy-pasting and clicks from auto-complete suggestions. There's a momentary appearance of the special characters but will disappear immediately.
更增强的形式是:
因为它也允许您输入空格,并且它只会针对输入文本的输入字段,并且不会打扰其他输入字段,例如电子邮件、密码等,因为通常我们在电子邮件和密码字段中需要特殊字符
A more enhanced form would be:
Because it will allow you to enter space as well and it will only target the input fields with type text and wont bother the other input fields like email, password etc as normally we need special characters in email and password field