PDA

Click to See Complete Forum and Search --> : JavaScript - Currency Function


Virus00110
Feb 28th, 2003, 09:28 PM
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 (http://www.quantumsoftware.net/test/test.htm)

Give it a try. Hope you like it. :D

Travis G
Jan 27th, 2004, 01:34 PM
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.


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.

tech-whiz
Jan 11th, 2005, 02:20 PM
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, ""));