I'm trying to use Javascript to verify that user input is a valid monetary (dollars and cents) amount, with no more than two digits to the right of the decimal point.
Shifting left, then performing a modulo 1 fails because of inherent floating-point issues:
(parseFloat("19.99") * 100) % 1 // Returns 0.9999999999997726
I'm aware of the BigDecimal library, but I'm not sure that converting to BigDecimal and then doing the validation would be the right approach, since it could cover up invalid inputs as well as floating-point issues.
As it stands, my workaround is to test the raw (string) input against the following regex:
/^\d*((\.\d{0,2})?)$/
Is this sufficient to guarantee that the input is a valid float in currency format? And if not, is there a math-based way to do the validation?
.7and7.allowed?