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;
Solved! Go to Solution.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
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.
Views
Replies
Total Likes
That did it, thanks. Makes sense now that I see it.
Views
Replies
Total Likes