JavaScript - Currency Function-VBForums
Results 1 to 3 of 3

Thread: JavaScript - Currency Function

  1. #1

    Thread Starter
    Registered User Virus00110's Avatar
    Join Date
    Jul 2002
    Location
    Williamsport, PA
    Posts
    290

    Post JavaScript - Currency Function

    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.
    Attached Files Attached Files

  2. #2
    Addicted Member
    Join Date
    Sep 2002
    Location
    Durham, NC, US
    Posts
    218
    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)

  3. #3
    New Member
    Join Date
    Jan 2005
    Posts
    1

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.