tags:

views:

32

answers:

4

Just a quick one here, does anyone know of a good regular expression for a percentage? Including 2 decimal places, i.e. 15.22%. I'm looking to put it inside a regularexpressionvalidator in ASP.NET.

Thanks

A: 

Updated to reflect @Lucero's input:

Forcing 2 decimals

^[0-9]+\.[0-9]{2}%$

Allowing either 2 decimals (with decimal) or integer. Both followed by percent.

^[0-9]+(\.[0-9]{2})?%$
Brad
A: 

This accepts 0.00%-100.00% including any number of leading zeros:

^0*(100\.00|[0-9]?[0-9]\.[0-9]{2})%$
Lucero
A: 
(\d+(\.\d+)?%)

That should work.

Ruel
A: 
\d\d\.\d\d%

To make the decimal portion optional:

\d\d(\.\d\d)?%

If single digit values are not padded with zeros:

\d{1,2}(\.\d\d)?%

Finally, to allow 100%

(100|\d{1,2}(\.\d\d)?)%
Amarghosh