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.

5
  • Before you downvote, meta.stackexchange.com/questions/66377/what-is-the-xy-problem Commented Jul 30, 2013 at 16:47
  • If your intention is to learn regex, go for it. Otherwise, why wont you just count the number of characters? Commented Jul 30, 2013 at 16:48
  • why wont you just count the number of characters? Commented Jul 30, 2013 at 16:49
  • I'm listening to other solutions. The problem is that I have a slick valdation handling system in place. I just want to integrate this solution in to the jquery validation package I have already. If I were to count the characters, how you folks suggest I handle that? Commented Jul 30, 2013 at 16:51
  • I'm not super familiar with whatever you're using, but it would be along the lines of this: var validLengths = [ 7, 9, 12, 13, 15 ]; and you can test $.inArray( $('.someInput').val().length, validLengths ) > -1 Commented Jul 30, 2013 at 16:57

1 Answer 1

1

You can specify exact number of characters by .{n}, where n is the number of characters that . matches. {n,} notation you used in third example means n or more. Combining that with your examples you can build a regexp that looks like ^(.{7}|.{9}|.{12}|.{13}|.{15})$.

Sign up to request clarification or add additional context in comments.

5 Comments

Can you explain why many examples encapsulate the statement like /^...$/ instead of without the backslashes like you recommended? I tried something very similar to your solution with the backslashes and it didn't work.
Slashes are used as a most common delimiter for regexp in many programming languages. They are not the part of regular expression but just the notation where it starts and where it ends, so to speak. Character used as a delimiter (in this case /) cannot appear in regexp without escaping them.
is that to say that in documentation the /'s are used so the reader understands how the regex works? Are the /'s ever included in actual code or are they just an aid for human eyes in documentation?
When i talked about notation i meant the syntax of the programming languages, so they are used in code. For example: $string =~ /[a-z]/. I'm sorry if i confused you, many people do put them inside // and there is no real difference, it is just a matter of syntax.
For reader i don't think they are useful since they appear at the beginning and the and, but you should include them if you want to specify some modificators such as /[a-z]/i for case-insensitive matching, or you can write them always.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.