Expand my Community achievements bar.

Don’t miss the AEM Skill Exchange in SF on Nov 14—hear from industry leaders, learn best practices, and enhance your AEM strategy with practical tips.
SOLVED

Validating multiple patterns

Avatar

Former Community Member

I am trying to have LC validate three different patterns in a text field. One would be one letter followed by 6 numbers, two is 6 numbers, and three is 9 numbers. This is what I have so far. It's close, but not quite doing what I need. It is correctly validating one, but with the two and three it is allowing anything between 6 and 9 numbers, not just 6 or 9 numbers. Suggestions?

var

r = new RegExp();

     r.compile("^[a-zA-Z]+[0-9]{6}|[0-9]{6}|[0-9]{9}$"

,"i");

var

result = r.test(this.rawValue);

if

(result == true)

true;

else

false;

1 Accepted Solution

Avatar

Correct answer by
Level 10

I think you need to group your regex to get the start of string and end of string anchor to work, try

^([a-zA-Z]+[0-9]{6}|[0-9]{6}|[0-9]{9})$

Which has brackets after the start of string anchor and before the end of string anchor.

Without the brackets the start of string anchor applies to the first pattern and the end of string anchor applies to the second pattern so the middle pattern is allowed to match string of 6, 7 or 8 numbers.

View solution in original post

2 Replies

Avatar

Correct answer by
Level 10

I think you need to group your regex to get the start of string and end of string anchor to work, try

^([a-zA-Z]+[0-9]{6}|[0-9]{6}|[0-9]{9})$

Which has brackets after the start of string anchor and before the end of string anchor.

Without the brackets the start of string anchor applies to the first pattern and the end of string anchor applies to the second pattern so the middle pattern is allowed to match string of 6, 7 or 8 numbers.

Avatar

Former Community Member

That did it, thanks. Makes sense now that I see it.