-
Feb 28th, 2003, 09:28 PM
#1
Thread Starter
Registered User
-
Jan 27th, 2004, 01:34 PM
#2
Addicted Member
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.
Code:
function isUSCurrency (sString) {
return RegExp(/^\$?\d+(\.\d{2})?$/).test(String(sString).replace(/^\s+|\s+$/g, ""));
}
It can be modified to meet specific needs or tolerances.
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)
-
Jan 11th, 2005, 02:20 PM
#3
New Member
Re: JavaScript - Currency Function
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, ""));
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|