0

I've got the following addMethod for jquery validation:

    $.validator.addMethod(
        "regex",
        function(value, element, regexp) {
            var re = new RegExp(regexp);
            return this.optional(element) || re.test(value);
        },
        "Invalid number of characters entered."
    );

And in my field I want to validate that the user enters 7, 9, 12, 13, or 15 chars, I can't get the regex to work. I've tried each of the following with their corresposing results:

"......." - Validates that 7 chars are entered

".......| ........." - Validates that 7 chars are entered but claims error when 9 are entered.

'/^([a-z0-9]{7,}|[a-z0-9]{9,})$/' - Fails to validate anything.

I realize there are plenty of resources out there but this is my first use of regex and I can't seem to put the right combination together. Please help if you see a solution. Thanks.

4

1 回答 1

1

您可以通过 指定确切的字符数.{n},其中 n 是.匹配的字符数。{n,}您在第三个示例中使用的符号表示n 或更多。将其与您的示例相结合,您可以构建一个看起来像^(.{7}|.{9}|.{12}|.{13}|.{15})$.

于 2013-07-30T16:54:04.753 回答