Skip to content

Instantly share code, notes, and snippets.

@jonkemp
Last active June 8, 2023 17:29
Show Gist options
  • Save jonkemp/9094324 to your computer and use it in GitHub Desktop.
Save jonkemp/9094324 to your computer and use it in GitHub Desktop.
Currency validation method for the jQuery Validation plugin. Decimal place is optional but if included, it requires 2 places. Also, the dollar sign is optional.
// Validation method for US currency
$.validator.addMethod("currency", function (value, element) {
return this.optional(element) || /^\$(\d{1,3}(\,\d{3})*|(\d+))(\.\d{2})?$/.test(value);
}, "Please specify a valid amount");
@jsharf-firstam
Copy link

Excellent work.
However, if I type a letter into the field by mistake, and then tab out to the next field, it fills the current field with "NaN" in addition to displaying the error message. Is there a way to keep the NaN from showing up in the text field?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment