拒绝特殊字符jQuery验证

编程入门 行业动态 更新时间:2024-10-09 22:23:06
本文介绍了拒绝特殊字符jQuery验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我需要使用插件jQuery Validate验证文本区域,为此我使用了正则表达式,并向插件添加了一个方法:

I need to validate a textarea using the plugin jQuery Validate, to do that I use a regex and I add a method to the plugin:

$.validator.addMethod( "regex", function(value, element, regexp) { var check = false; var re = new RegExp(regexp); return this.optional(element) || re.test(value); }, "No special Characters allowed here. Use only upper and lowercase letters (A through Z; a through z), numbers and punctuation marks (. , : ; ? ' ' \" - = ~ ! @ # $ % ^ & * ( ) _ + / < > { } )" );

然后在选项中添加正则表达式:

Then in the options I add the regex:

comments:{ required: true, maxlength: 8000, regex: /[^A-Za-z\d\-\=\~\!@#\%&\*\(\)_\+\\\/<>\?\{\}\.\$‘\^\+\"\';:,\s]/ }

此功能"以某种方式起作用,它确实检测到无效字符并显示消息,问题是它仅在特殊字符是框中唯一的字符时才起作用,例如:

This "works" in a certain way, it does detect the invalid characters and displays the message, the problem is it only works when the special characters are the only ones in the box, for instance:

| `` ° ¬ // This shows the error message but... test | // This won't show the message

因此,如果存在一个允许的字符,则验证将停止工作.我想念什么吗?

So if one allowed character is there then the validation just stops working. Am I missing something?

P.S.我很确定这与插件有关,因为我仅使用javascript测试了正则表达式,并且效果很好.

P.S. I'm pretty sure this has something to do with the plugin 'cause I've tested the regex with just javascript and it works well.

推荐答案

而不是测试特殊字符的存在,而是仅测试有效字符的存在

rather than testing for the presence of the special characters, test for only presence of valid characters

regex: /^[A-Za-z\d=#$%...-]+$/

用您要允许的所有特殊字符替换....在上面的示例中,将允许#,$,%和-.注意:您不必转义[]内的大多数字符.

Replace ... with all the special characters you want to allow. In the example above, #, $, %, and - would be allowed. Note: you don't have to escape (most) of the characters inside of the [].

如果您想允许-,则它必须是最后一个字符,否则regex会尝试解析范围. (例如[a-c]匹配a,b和c.[a-c-]匹配a,b,c和-)

If you'd like to allow a -, it needs to be the last character otherwise regex tries to parse a range. (e.g., [a-c] matches a, b, and c. [a-c-] matches a, b, c, and -)

此外,如果您想允许^,则不能将其作为第一个字符,否则regex会将其视为not运算符. (例如,[^abc]匹配非a,b或c的任何字符)

Also, if you'd like to allow a ^, it cannot be the first character otherwise regex treats this as a sort of not operator. (e.g., [^abc] matches any character that's not a, b, or c)

在上面的示例中,完整的正则表达式可能看起来像这样

In your example above, the complete regex might look something like this

regex: /^[A-Za-z\s`~!@#$%^&*()+={}|;:'",.<>\/?\\-]+$/

说明

NODE EXPLANATION -------------------------------------------------------------------------------- ^ the beginning of the string -------------------------------------------------------------------------------- [A-Za- any character of: 'A' to 'Z', 'a' to 'z', z\s`~!@#$%^&*()+={}| whitespace (\n, \r, \t, \f, and " "), '`', ;:'",.<>/?\\-]+ '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '+', '=', '{', '}', '|', ';', ':', ''', '"', ',', '.', '<', '>', '/', '?', '\\', '-' (1 or more times (matching the most amount possible)) -------------------------------------------------------------------------------- $ before an optional \n, and the end of the string

更多推荐

拒绝特殊字符jQuery验证

本文发布于:2023-11-27 18:40:36,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1639151.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:特殊字符   jQuery

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!