I am trying to build a regex which will allow both negative and positive decimal numbers with the following rules.
It is pretty straightforward since 12 - 9 == 3 for two decimals + the dot.
var re = new RegExp('^-?\\d{1,9}(\\.\\d{1,2})?$');
authorizes
-123456789
-123456789.1
-123456789.12
0
0.12
but will not accept
01234567890123
more than 12 decs123.
a dot without decimals123.123
more than two decimals.
or .12
(missing 0)My own regex:
var rgx = /^(-{1}?(?:([0-9]{0,10}))|([0-9]{1})?(?:([0-9]{0,9})))?(?:\.([0-9]{0,3}))?$/;
var NumberToValidate = 48428;
var valid = /^([0-9]*[1-9][0-9]*)$/.test(NumberToValidate);
{
if (!valid)
alert("Invalid Quantity!!\nThe Quantity cannot be Zero\n or a part of a Unit !");
return false;
}
Check this regex.
^[+-]?[0-9]{1,9}(?:\.[0-9]{1,2})?$
This regex says
/(?:^(?:(?:[1-9][0-9]{1,})|0)\.[0-9]{1,}$)|^[1-9]+[0-9]*$/