jQuery validate: How to add a rule for regular expression validation?

前端 未结 13 1528
忘掉有多难
忘掉有多难 2020-11-22 07:41

I am using the jQuery validation plugin. Great stuff! I want to migrate my existing ASP.NET solution to use jQuery instead of the ASP.NET validators. I am m

13条回答
  •  南旧
    南旧 (楼主)
    2020-11-22 08:36

    No reason to define the regex as a string.

    $.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 },
    

    tis better this way, no?

提交回复
热议问题