I want to test for this input: [an optional negative sign] 2 digits [an optional . and an optional digit] like this:
-34
or -34.5333
or 34.53333
or 34
in JavaScript
This is what I came up with but it doesn't work!
/^(-)\d{2}(\.d\{1})$/
Can someone please help me?
To use negative numbers, just place a minus (-) character before the number we want to turn into a negative value: let temperature = -42; What we've seen in this section makes up the bulk of how we will actually use numbers.
If UNSIGNED is used with DECIMAL , negative values are not allowed. MySQL stores numbers in DECIMAL columns as strings. Therefore, numbers outside the maximum numeric range for this data type may be stored in a DECIMAL column.
Definition and Usage The Math. sign() method retuns whether a number is negative, positive or zero. If the number is positive, this method returns 1. If the number is negative, it returns -1.
Try this regex:
/^-?\d{2}(\.\d+)?$/
this regex matches any valid integer.
/^0$|^-?[1-9]\d*(\.\d+)?$/
you can modify this to suite your needs :
/^-?[1-9]\d{0,1}(\.[1-9]{1})?$/
this matches 2.1, 21.4, 3, 90...
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