morden.dk
morden.dk

Reputation: 169

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.

Upvotes: 5

Views: 18722

Answers (2)

Interreto
Interreto

Reputation: 122

It is correct regex for positive digits.

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

The previous answer not correct for 0123.

Upvotes: 6

ruakh
ruakh

Reputation: 183251

You can write:

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

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

Upvotes: 20

Related Questions