Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript regex for allowing only positive digits

I use this to test for digits

/^\d+$/

But I need to make sure that it's greater than zero, while still allowing 0000123123123 for instance.

like image 423
morden.dk Avatar asked Nov 29 '22 17:11

morden.dk


2 Answers

You can write:

/^\d*[1-9]\d*$/

(zero or more digits, followed by a nonzero digit, followed by zero or more digits).

like image 69
ruakh Avatar answered Dec 06 '22 10:12

ruakh


It is correct regex for positive digits.

 /^[1-9]\d*$/g 

The previous answer not correct for 0123.

like image 29
Interreto Avatar answered Dec 06 '22 08:12

Interreto