ibjdt
Programmer
- Nov 25, 2002
- 63
i am trying to validate a price field as any number of numerals (but not required like $.99) followed by a decimal (but no required like $9)followed by 1 or 2 numerals (but not required like $9 or $9.).
i start by removing leading and trailing white space and stripping spaces, commas and $ sign, then check what's left
if the value passes i use sprintf to properly format to 2 decimals.
everything works except when 3 numerals are entered after the decimal it passes.
why doesn't it fail since i include {1,2} in the code?
thanks.
i start by removing leading and trailing white space and stripping spaces, commas and $ sign, then check what's left
Code:
# should only contain numbers and decimals
$price =~ m/[^0-9\.]/
# should follow the pattern described above.....
$price =~ m/([0-9]+)?(\.{1})?([0-9]{1,2})?/
if the value passes i use sprintf to properly format to 2 decimals.
everything works except when 3 numerals are entered after the decimal it passes.
why doesn't it fail since i include {1,2} in the code?
thanks.