0

I need a regex for the ASP.Net (4) Regex Validation control. It needs to be a RegEx validator to support other dynamic behaviors outside the scope of this post..

I was using the following, but it fails if the user enters the % sign following the number (which is a req of my spec):

^(100(?:\.0{1,2})?|0*?\.\d{1,2}|\d{1,2}(?:\.\d{1,2})?)$

I tried adding an atomic group of ^(?>%?) at the end, with no luck, after reading the excellent post

Regular expression greedy match not working as expected

Does anyone have any ideas?

Ronan Boiteau
  • 9,608
  • 6
  • 34
  • 56
BaldEagle
  • 5
  • 4

2 Answers2

1

Try this

^(100(?:.0{1,2})?%?|0*?.\d{1,2}%?|\d{1,2}(?:.\d{1,2})?%?)$
Aliostad
  • 80,612
  • 21
  • 160
  • 208
  • Thanks, that works for numbers up to and including 100.00%! I actually need to let in higher numbers, as 'percent of quota' is one possible metric which frequently exceeds 100%. Sorry, I should have explained that! – BaldEagle Jan 25 '11 at 16:24
  • this one accepts 22222 which is wrong .. you can test it here:http://regex101.com/ – Yousi Sep 03 '14 at 10:50
0

try this one instead:

^0*(100(\.00?)?|[0-9]?[0-9](\.[0-9][0-9]?)?)%?$
Yousi
  • 811
  • 3
  • 12
  • 26