Nice little US currency validation function. Nice to use when wanting to validate a textbox on a web form that is for the user to enter a dollar amount.
Live working version
Give it a try. Hope you like it.![]()
Nice little US currency validation function. Nice to use when wanting to validate a textbox on a web form that is for the user to enter a dollar amount.
Live working version
Give it a try. Hope you like it.![]()
You have a very fancy solution.
I just thought I would provide a one-line alternative for those that didn't need the extra features or overhead.
It can be modified to meet specific needs or tolerances.Code:function isUSCurrency (sString) { return RegExp(/^\$?\d+(\.\d{2})?$/).test(String(sString).replace(/^\s+|\s+$/g, "")); }
Travis, Kung Foo Journeyman
Web Standards: HTML 4.01, CSS Level 2, ECMA 262 Bindings to DOM Level 1, JavaScript 1.5 Guide and Reference
Perl: Documentation, Learn Perl, Llama, Camel, Cookbook, Perl Monks, Perl Mongers, O'Reilly's Perl.com, ActiveState, CPAN, TPJ, and use Perl;
OSS: Mozilla, MySQL (Manual)
Thanks for the post Travis! It was a great starting point.
I've tweaked Travis' RegExp a little to allow for commas:
bIsValidCurrency = RegExp(/^\$?[0-9\,]+(\.\d{2})?$/).test(String(txtValue).replace(/^\s+|\s+$/g, ""));
You could also allow for negative currency values with:
bIsValidCurrency = RegExp(/^-?\$?[0-9\,]+(\.\d{2})?$/).test(String(txtValue).replace(/^\s+|\s+$/g, ""));