-1

I have the requirement to restrict a non-required textbox to only numbers (0-9) with a separator of ';'. The pattern is that the groups can be 4 or 5 in length and can repeat n times. Anything less than 4 in a group is invalid. After 4 or 5 I need to check for the existence of a separator character ';'. This pattern can repeat n times. I have tried variations of but this doesn't seem to be working. Something simple to start out like

[0-9]{4,5};+

is invalid as I don't need the separator for only 1 number grouping.

Next I tried

^[0-9]{4,5}|[0-9]{4,4};|[0-9]{5,5};$

but this doesn't work because the existence of four digits like 1111 or five digits 11111 before gives one match before it goes awry example "11111;j" Is there a way in a regex to validate

1111
11111
1111;1111
11111;1111
11111;11111

but catch

111
111;
1111;1
11111;1
abc

in a repeating fashion?

sshashank124
  • 31,495
  • 9
  • 67
  • 76
Robin
  • 33
  • 1
  • 4

1 Answers1

0

This validate your examples.

^[0-9]{4,5}(;[0-9]{4,5})?$

Try it

It's not clare what you mean by "in a repeating fashion". If you want validate also this

1111;11111;11111;1111;11111

You can use this regex

^[0-9]{4,5}(;[0-9]{4,5})*$

Try it

blow
  • 12,811
  • 24
  • 75
  • 112
  • Your assumption was correct. I have to account for nothing entered, one 4-5 digit entry or an indeterminate number of entries that are 4 or 5 digits in length and separated by a semi colon. – Robin Jan 05 '20 at 17:31