拒绝特殊字符jQuery验证 [英] denying special characters jQuery Validate

查看:138
本文介绍了拒绝特殊字符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验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆