Reputation: 799
I need to validate a number with those criterias :
I tried like this :
pattern="/^[-+]?[0-9]\d*(\.\d+)?$/i",
Some examples :
Thx in advance.
Upvotes: 2
Views: 44
Reputation: 626845
You may use
'/^[-+]?(?:[1-9]\d{0,3}|0)(?:\.\d{1,2})?$/'
See the regex demo.
Details
^
- start of string[-+]?
- either -
or +
(?:[1-9]\d{0,3}|0)
- a non-capturing group matching either
[1-9]\d{0,3}
- a digit from 1
to 9
(non-zero) and any 0 to 3 digits|
- or0
- a zero(?:\.\d{1,2})?
- an optional non-capturing group matching 1 or 0 occurrences of
\.
- a dot\d{1,2}
- 1 or 2 digits$
- end of string.Upvotes: 2