I've got a Javascript function that tests a regexp that I'm intending to validate positive decimals up to a precision of two:
function isPositiveDecimalPrecisionTwo(str) {
return /^\d*(\.\d{1,2}$)?/.test(str);
}
I'm not getting the results I'm expecting when I call it from code.
For example:
var a = isPositiveDecimalPrecisionTwo("1"); // expect t, returns t
var b = isPositiveDecimalPrecisionTwo("1.2"); // expect t, returns t
var c = isPositiveDecimalPrecisionTwo("1.25"); // expect t, returns t
var d = isPositiveDecimalPrecisionTwo("1.257"); // * expect f, returns t *
var e = isPositiveDecimalPrecisionTwo("1.2575"); // * expect f, returns t *
var f = isPositiveDecimalPrecisionTwo(".25"); // * expect t, returns f *
var g = isPositiveDecimalPrecisionTwo("d"); // expect f, returns f
var h = isPositiveDecimalPrecisionTwo("d1"); // expect f, returns f
var i = isPositiveDecimalPrecisionTwo("1d"); // * expect f, returns t *
var j = isPositiveDecimalPrecisionTwo("1d1"); // * expect f, returns t *
var k = isPositiveDecimalPrecisionTwo("-1"); // expect f, returns f
var l = isPositiveDecimalPrecisionTwo("-1.5"); // expect f, returns f
There are three issues:
- Validating "1.257" or "1.2575" returns true, which I though would
return false due to the
\d{1,2}
- Validating ".25" returns false, which I thought would return true due to the
^\d*
- Validating "1d" or "1d1" return true. It looks like
\.
is reading as any character when it looks to me like it's a properly escaped "." (dot).
However, when I use a tool like regexpal, the same regexp appears to be validating the way I expect:
/?flags=g®ex=%5E%5Cd%2B(%5C.%5Cd%7B1%2C2%7D%24)%3F&input=
What am I missing?
I've got a Javascript function that tests a regexp that I'm intending to validate positive decimals up to a precision of two:
function isPositiveDecimalPrecisionTwo(str) {
return /^\d*(\.\d{1,2}$)?/.test(str);
}
I'm not getting the results I'm expecting when I call it from code.
For example:
var a = isPositiveDecimalPrecisionTwo("1"); // expect t, returns t
var b = isPositiveDecimalPrecisionTwo("1.2"); // expect t, returns t
var c = isPositiveDecimalPrecisionTwo("1.25"); // expect t, returns t
var d = isPositiveDecimalPrecisionTwo("1.257"); // * expect f, returns t *
var e = isPositiveDecimalPrecisionTwo("1.2575"); // * expect f, returns t *
var f = isPositiveDecimalPrecisionTwo(".25"); // * expect t, returns f *
var g = isPositiveDecimalPrecisionTwo("d"); // expect f, returns f
var h = isPositiveDecimalPrecisionTwo("d1"); // expect f, returns f
var i = isPositiveDecimalPrecisionTwo("1d"); // * expect f, returns t *
var j = isPositiveDecimalPrecisionTwo("1d1"); // * expect f, returns t *
var k = isPositiveDecimalPrecisionTwo("-1"); // expect f, returns f
var l = isPositiveDecimalPrecisionTwo("-1.5"); // expect f, returns f
There are three issues:
- Validating "1.257" or "1.2575" returns true, which I though would
return false due to the
\d{1,2}
- Validating ".25" returns false, which I thought would return true due to the
^\d*
- Validating "1d" or "1d1" return true. It looks like
\.
is reading as any character when it looks to me like it's a properly escaped "." (dot).
However, when I use a tool like regexpal., the same regexp appears to be validating the way I expect:
http://regexpal./?flags=g®ex=%5E%5Cd%2B(%5C.%5Cd%7B1%2C2%7D%24)%3F&input=
What am I missing?
Share Improve this question edited Nov 14, 2012 at 15:46 ruakh 184k29 gold badges290 silver badges323 bronze badges asked Nov 14, 2012 at 15:16 dfran02dfran02 1631 gold badge2 silver badges9 bronze badges2 Answers
Reset to default 7The problem is that your $
is inside the parenthesis. You want it to be after the group.
/^\d*(\.\d{1,2})?$/
This should work the way you expect.
With the $
inside, it was matching everything that started with a number, since the end of string ($
) was "optional".
You can also do it without grouping the decimal portion by just adding a ?
after the escaped decimal point
^\d*\.?\d{1,2}$