jQuery 验证正则表达式以禁止数字和特殊字符

jQuery validate regex to disallow numerics and special characters

我有一个表单要求字段不能包含任何特殊字符或数字。目前它适用于:

问题是当我添加 #$%abcabc123 之类的内容时,它不会产生我预期的错误。

我使用的函数如下所示:

$.validator.addMethod("checkallowedchars", function (value) {
    var pattern = new RegExp('[A-Za-z]+', 'g');
    return pattern.test(value)
}, "The field contains non-admitted characters");

JSFiddle 显示 function/regex 我正在使用:http://jsfiddle.net/nmL8maa5/

使用这个:

^[A-Za-z]+$

解释:

  1. ^ 是开始锚点的字符串。
  2. $是字符串结束锚。

正则表达式应该使用它从头到尾处理字符串,不包括中间部分。

其中一个正确答案是:

return /^[A-Z]+$/i.test(value);

并添加

checkallowedchars: true,

遵守规则。

请参阅 updated demo fiddle(下面的那个对我不起作用,不知道为什么)。

$(document).ready(function () {
    
    $.validator.addMethod("pwcheckallowedchars", function (value) {
        return /^[a-zA-Z0-9!@#$%^&*()_=\[\]{};':"\|,.<>\/?+-]+$/.test(value) // has only allowed chars letter
    }, "The password contains non-admitted characters");

    $.validator.addMethod("checkallowedchars", function (value) {
        return /^[A-Z]+$/i.test(value);
    }, "The field contains non-admitted characters");

    $.validator.addMethod("pwcheckspechars", function (value) {
        return /[!@#$%^&*()_=\[\]{};':"\|,.<>\/?+-]/.test(value)
    }, "The password must contain at least one special character");
    
    $.validator.addMethod("pwcheckconsecchars", function (value) {
        return ! (/(.)/.test(value)) // does not contain 3 consecutive identical chars
    }, "The password must not contain 3 consecutive identical characters");

    $.validator.addMethod("pwchecklowercase", function (value) {
        return /[a-z]/.test(value) // has a lowercase letter
    }, "The password must contain at least one lowercase letter");
    
    $.validator.addMethod("pwcheckrepeatnum", function (value) {
        return /\d{2}/.test(value) // has a lowercase letter
    }, "The password must contain at least one lowercase letter");
    
    $.validator.addMethod("pwcheckuppercase", function (value) {
        return /[A-Z]/.test(value) // has an uppercase letter
    }, "The password must contain at least one uppercase letter");
    
    $.validator.addMethod("pwchecknumber", function (value) {
        return /\d/.test(value) // has a digit
    }, "The password must contain at least one number");
    
    
    
    
    
    $('#myform').validate({
        // other options,
        rules: {
            "firstname.fieldOne": {
                required: true,
                checkallowedchars: true,
                pwchecklowercase: true,
                pwcheckuppercase: true,
                pwchecknumber: true,
                pwcheckconsecchars: true,
                pwcheckspechars: true,
                pwcheckallowedchars: true,
                minlength: 8,
                maxlength: 20
            }
        }
    });

});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/jquery.validate.js"></script>
<script src="http://ajax.aspnetcdn.com/ajax/jquery.validate/1.13.1/additional-methods.js"></script>
<form id="myform">
    <input type="text" name="firstname.fieldOne" /><br/>
    <br/>
    <input type="submit" />
</form>

您的代码中有两个问题:

  • 正则表达式未锚定,当它未锚定时,它匹配较大字符串中的 部分 子字符串。由于正好匹配1+个字母,而#$%abc包含3个字母,满足条件
  • 第二个问题是您正在使用 /g 修饰符和 RegExp.text()This leads to unexpected behavior.