0

I have a list of valid numbes which looks like this:

50, 56, 62, 68, 74, 80, 86, 92, 98, 104, 110, 116, 122, 128, 134, 140, 146, 152, 158

I need an regular expression which allows every number in this list, but nothing else. This regex is used to validate a HTML5 input control. A few examples for clarification:

50 => true
150 => false
abc => false
51 => false
110 => true
11 => false
50, 56 => false

I have tried this expression, but the problem is that numbers like 156 and 150 also get matched.

50|56|62|68|74|80|86|92|98|104|110|116|122|128|134|140|146|152|158

Can anyone help me out?

Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563
Simon Schiller
  • 644
  • 1
  • 8
  • 23
  • Did you use it as `pattern="50|56|62|68|74|80|86|92|98|104|110|116|122|128|134|140|146|152|158"`? – Wiktor Stribiżew Oct 28 '16 at 08:15
  • Your regex works as is. There is no issue at all in the first place. Second, testing in the real target environment is a must before posting the question, online testers are misused regularly, they are only to be used when you know what you are doing. Regex101.com also provides sample code generation, but HTML5 pattern has its own peculiarities. – Wiktor Stribiżew Oct 28 '16 at 08:45

1 Answers1

6

Add ^ and $ to match begining and ending of string

^(50|56|62|68|74|80|86|92|98|104|110|116|122|128|134|140|146|152|158)$

Edit

OP has mentioned in the comment that he tested his pattern using online tool and got wrong result. Also Wiktor Stribiżew mentioned in the comment that OP's pattern is correct if that pattern is used in pattern attribute of HTML 5 Input element.

Niyoko
  • 7,512
  • 4
  • 32
  • 59