tags:

views:

3478

answers:

5

I need an regular expression pattern to only accept positive whole numbers. It can also accept a single zero.

I do not want to accept decimals, negative number and numbers with leading zeros.

Any suggestions?

+2  A: 
/([1-9][0-9]*)|0/
Alex Brault
+1  A: 
/^0|[1-9]\d*$/
Robert Gamble
+2  A: 

"[1-9][0-9]*|0"

I'd just use "[0-9]+" to represent positive whole numbers.

Cheery
+10  A: 
^(0|[1-9][0-9]*)$
Federico Ramponi
Should it include an optional comma, in case the format allows it (like 1,000,000)?
Ben
No, It should not accept commas.
Michael Kniskern
This would do the job: ^(0|[1-9][0-9]*|[1-9][0-9]{0,2}(,[0-9]{3,3})*)$
Federico Ramponi
+2  A: 

My favorite: link text

n8wrl