I am trying to build a regex which will allow both negative and positive decimal numbers with the following rules.
Can anyone help me out? thanks a lot in advance.
A number, which is a negative or positive dot-separated decimal, or a negative or positive integer. Suitable to check for input of numerical values. Regex Tester isn't optimized for mobile devices yet. You can still take a look, but it might be a bit quirky. > Okay! Regex Tester requires a modern browser.
A regular expression for a decimal number needs to checks for one or more numeric characters (0-9) at the start of the string, followed by an optional period, and then followed by zero or more numeric characters (0-9). This should all be followed by an optional plus or minus sign.
And finally, to ensure that this regex matches only a decimal number with nothing in front and nothing behind it, we can add the start-of-string (^) and end-of-string ($) characters to the start and end, respectively:
Use number input is the best way for your problem. But if you really want use regex you can use one of two regex above.
Check this regex.
^[+-]?[0-9]{1,9}(?:\.[0-9]{1,2})?$
This regex says
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With