jQuery 如何限制某个表单域中的特殊字符
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25448650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to restrict special characters in a certain form field
提问by NewbieDeveloper
I have html which is something like, not exactly like this, but just to give you an idea.
我有类似的 html,不完全像这样,但只是为了给你一个想法。
<input type="text" disabled="disabled" name="ip" />
<input type="text" name="name" />
<input type="text" name="email" />
<input type="submit" value="send" />
How to restrict special characters from using in only name field? I want only alphanumeric in name field.
如何限制特殊字符仅在名称字段中使用?我只想要名称字段中的字母数字。
Can you please help me do this?
你能帮我做这个吗?
Thanks in advance
提前致谢
回答by Suganth G
Try this:
尝试这个:
var alphanumers = /^[a-zA-Z0-9]+$/;
if(!alphanumers.test($("#firstname").val())){
alert("Nickname can have only alphabets and numbers.");
}
DEMO
演示
or
或者
$(document).ready(function () {
var charReg = /^\s*[a-zA-Z0-9,\s]+\s*$/;
$('.keyup-char').keyup(function () {
$('span.error-keyup-1').hide();
var inputVal = $(this).val();
if (!charReg.test(inputVal)) {
$(this).parent().find(".warning").show();
} else {
$(this).parent().find(".warning").hide();
}
});
});
DEMO2
演示2
回答by Arthur
You can search on Google about "input mask" like this very good plugin : https://github.com/RobinHerbots/jquery.inputmask
你可以在谷歌上搜索关于“输入掩码”这样的非常好的插件:https: //github.com/RobinHerbots/jquery.inputmask
Or this one too : http://digitalbush.com/projects/masked-input-plugin/
或者这个也是:http: //digitalbush.com/projects/masked-input-plugin/