Last active
June 8, 2023 17:29
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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"); |
it doesnt seem that the dollar sign is optional, it wont accept a value from me without it
@Jrizzi1 - Try removing the ^$ from the regex - it should work then without the $ sign
If you want to have the $ as an optional element, add a ? after the ^$
Nice work, thanks 😄
How to fix min and max as well?
How to fix min and max as well?
@rvpatel
You can add those to the rule:
https://jqueryvalidation.org/min-method/
https://jqueryvalidation.org/max-method/
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
nice!