0

I want to test the content of a particular text field for the following rules:

  1. It should be number with 5 digits and a caret symbol at the end of each number.
  2. The above can occur only 4 times and the entire content should always end with number.

Examples:

Valid

  • 11111^11111
  • 11111

Invalid

  • 11111^11111^1111
  • 11111^11111^11111^11111^11111
  • 11111^11111^11111^11111^
  • 11111^

I am trying to implement the same using regex, and tried:

/^([0-9]{5}\^){1,4}$/g
1
  • 2
    Your question is confusing. You list invalid groups as valid. Which do you want? Commented Sep 11, 2012 at 23:59

1 Answer 1

5

Try the following:

/^\d{5}(\^\d{5}){0,3}$/

It matches 5 digits at the beginning of the line ^\d{5}, followed by zero to three {0,3} groups of a caret and 5 digits (\^\d{5}), thus guaranteeing that you can only have 4 groups.

DEMO.

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

7 Comments

More than one group is supposed to be invalid, by the question.
@Daedalus: His first test has two groups and it's valid: 11111^11111(valid).
As of this writing, your regex would match the invalid groups as well. See here.
@Daedalus: Indeed, it should be three groups instead of four. I've edited my answer, thanks
@Daedalus: I admit the question is a bit confusing, but I think based on OP's examples and his intro, he basically wants to match groups of 5 digits separated by a caret, and up to 4 groups. This interpretation, I think, reflects the introduction as well as the examples and my regex.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.