validator.js icon indicating copy to clipboard operation
validator.js copied to clipboard

Add USD($) validator

Open benjsmi opened this issue 11 years ago • 9 comments

Would like a validator that checks whether $10.03 is valid US dollars. Would accept 10.03, 1.39, .03, 0.10, $10.01, $0.01, etc, even 39¢

Thanks!

benjsmi avatar Feb 10 '15 20:02 benjsmi

@chriso is this appropriate for this library or is it too specialized? Maybe a more generic validator that takes a currency code? I'd be happy to contribute if you think this belongs.

regularmike avatar Feb 23 '15 23:02 regularmike

Would be completely happy with generic currency validator.

benjsmi avatar Feb 23 '15 23:02 benjsmi

A generic one would be nice; one that uses locales or currency codes like the isMobilePhone validator.

Some more info here:

  • https://msdn.microsoft.com/en-us/goglobal/bb688126.aspx

chriso avatar Feb 24 '15 01:02 chriso

Cool, I'm on it. I'll try to add support for currency codes corresponding to those phone locales, plus USD.

regularmike avatar Feb 24 '15 01:02 regularmike

@programsam just curious, what's the use case for needing to validate "39¢"?

regularmike avatar Feb 24 '15 02:02 regularmike

No particular one in mind, but it is a valid representation for US currency, so, for completeness if nothing else. I imagine if you leave it unimplemented, there will be a request in the future.

benjsmi avatar Feb 24 '15 20:02 benjsmi

Agree that it's a valid representation for US currency, but the pull request I just sent implements a currency validator based on currency codes, including USD. Doesn't seem like that would be a valid format for USD. I would suggest combining with a simple "matches" validator if you want to include strings with cents for units.

regularmike avatar Feb 24 '15 21:02 regularmike

I like the PR the way it is just fine.

benjsmi avatar Feb 25 '15 18:02 benjsmi

Fixed by #368.

chriso avatar Mar 03 '15 02:03 chriso