我正在使用jQuery验证插件。伟大的东西!我想迁移我现有的ASP。NET解决方案使用jQuery代替ASP。净验证器。我缺少正则表达式验证器的替换。我希望能够做到这样:

$("Textbox").rules("add", { regularExpression: "^[a-zA-Z'.\s]{1,40}$" })

我如何添加一个自定义规则来实现这一点?


当前回答

感谢redsquare的回答,我添加了一个这样的方法:

$.validator.addMethod(
  "regex",
  function(value, element, regexp) {
    var re = new RegExp(regexp);
    return this.optional(element) || re.test(value);
  },
  "Please check your input."
);

现在你需要做的就是验证任何正则表达式:

$("#Textbox").rules("add", { regex: "^[a-zA-Z'.\\s]{1,40}$" })

此外,它看起来像有一个名为additional-methods.js的文件,其中包含方法“pattern”,当使用不带引号的方法创建时,它可以是一个RegExp。


Edit

pattern函数现在是执行此操作的首选方法,示例如下:

$("#Textbox").rules("add", { pattern: "^[a-zA-Z'.\\s]{1,40}$" })

https://cdnjs.com/libraries/jquery-validate https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/additional-methods.min.js

其他回答

    $.validator.methods.checkEmail = function( value, element ) {
        return this.optional( element ) || /[a-z]+@[a-z]+\.[a-z]+/.test( value );
    }

    $("#myForm").validate({
        rules: {
            email: {
                required: true,
                checkEmail: true
            }
        },
        messages: {
            email: "incorrect email"
        }
    });

这是工作代码。

function validateSignup()
{   
    $.validator.addMethod(
            "regex",
            function(value, element, regexp) 
            {
                if (regexp.constructor != RegExp)
                    regexp = new RegExp(regexp);
                else if (regexp.global)
                    regexp.lastIndex = 0;
                return this.optional(element) || regexp.test(value);
            },
            "Please check your input."
    );

    $('#signupForm').validate(
    {

        onkeyup : false,
        errorClass: "req_mess",
        ignore: ":hidden",
        validClass: "signup_valid_class",
        errorClass: "signup_error_class",

        rules:
        {

            email:
            {
                required: true,
                email: true,
                regex: /^[A-Za-z0-9_]+\@[A-Za-z0-9_]+\.[A-Za-z0-9_]+/,
            },

            userId:
            {
                required: true,
                minlength: 6,
                maxlength: 15,
                regex: /^[A-Za-z0-9_]{6,15}$/,
            },

            phoneNum:
            {
                required: true,
                regex: /^[+-]{1}[0-9]{1,3}\-[0-9]{10}$/,
            },

        },
        messages: 
        {
            email: 
            {
                required: 'You must enter a email',
                regex: 'Please enter a valid email without spacial chars, ie, Example@gmail.com'
            },

            userId:
            {
                required: 'Alphanumeric, _, min:6, max:15',
                regex: "Please enter any alphaNumeric char of length between 6-15, ie, sbp_arun_2016"
            },

            phoneNum: 
            {
                required: "Please enter your phone number",
                regex: "e.g. +91-1234567890"    
            },

        },

        submitHandler: function (form)
        {
            return true;
        }
    });
}

没有理由将正则表达式定义为字符串。

$.validator.addMethod(
    "regex",
    function(value, element, regexp) {
        var check = false;
        return this.optional(element) || regexp.test(value);
    },
    "Please check your input."
);

and

telephone: { required: true, regex : /^[\d\s]+$/, minlength: 5 },

这样更好,不是吗?

你可以使用addMethod()

e.g

$.validator.addMethod('postalCode', function (value) { 
    return /^((\d{5}-\d{4})|(\d{5})|([A-Z]\d[A-Z]\s\d[A-Z]\d))$/.test(value); 
}, 'Please enter a valid US or Canadian postal code.');

这里有好文章https://web.archive.org/web/20130609222116/http://www.randallmorey.com/blog/2008/mar/16/extending-jquery-form-validation-plugin/

感谢redsquare的回答,我添加了一个这样的方法:

$.validator.addMethod(
  "regex",
  function(value, element, regexp) {
    var re = new RegExp(regexp);
    return this.optional(element) || re.test(value);
  },
  "Please check your input."
);

现在你需要做的就是验证任何正则表达式:

$("#Textbox").rules("add", { regex: "^[a-zA-Z'.\\s]{1,40}$" })

此外,它看起来像有一个名为additional-methods.js的文件,其中包含方法“pattern”,当使用不带引号的方法创建时,它可以是一个RegExp。


Edit

pattern函数现在是执行此操作的首选方法,示例如下:

$("#Textbox").rules("add", { pattern: "^[a-zA-Z'.\\s]{1,40}$" })

https://cdnjs.com/libraries/jquery-validate https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/jquery.validate.min.js https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.19.2/additional-methods.min.js