简体   繁体   中英

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.

You can write:

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

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

It is correct regex for positive digits.

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

The previous answer not correct for 0123.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM