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. :D
Printable View
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. :D
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, ""));
}
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, ""));