I have this regex working but now need to allow numbers without the decimal as well
// Validate for 2 decimal for money
jQuery.validator.addMethod("decimalTwo", function(value, element) {
return this.optional(element) || /^(\d{1,3})(\.\d{2})$/.test(value);
}, "Must be in US currency format 0.99");
Currently this forces the user to at least have the .00 added to a number, would like it to allow both the current regex and whole numbers without the decimal.
would I just add the ? at the end of the second half of the RegEx?
// Validate for 2 decimal for money
jQuery.validator.addMethod("decimalTwo", function(value, element) {
return this.optional(element) || /^(\d{1,3})(\.\d{2})?$/.test(value);
}, "Must be in US currency format 0.99");
EDIT:
Ok but what if someone enters 1.2 ?
If what you want is for 1, 1.2, and 1.20 all to work:
/^(\d{1,3})(\.\d{1,2})?$/
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