New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add currency validator #368
Conversation
'CNY': /^¥?\-?([0-9]{1,3})?((,[0-9]{3})*|([0-9]{3})*)(\.[0-9]{2})?$/, | ||
'ZAR': /^(R|R )?\-?([0-9]{1,3})?(( [0-9]{3})*|([0-9]{3})*)(,[0-9]{2})?$/, | ||
'AUD': /^\$?\-?([0-9]{1,3})?((,[0-9]{3})*|([0-9]{3})*)(\.[0-9]{2})?$/, | ||
'HKD': /^\$?\-?([0-9]{1,3})?((,[0-9]{3})*|([0-9]{3})*)(\.[0-9]{2})?$/, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AUD, HKD and USD have the same regex patterns. Can you pull it out into a variable instead of repeating it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This matches a leading comma, e.g. ,123
or $-,123
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, was trying to modify to accept ".10". I see a few other issues with spaces and leading zeros. Will fix tomorrow and use a variable for dollar pattern. I thought maybe I would come across some variations with the different dollar formats but I haven't seen any yet.
Looks like some of these match empty string and the currency symbol by itself. Need to add some lookaheads, perhaps. Will update soon. I know I've been a bit overeager. |
I think this is fairly solid now. Added more tests for edge cases like the ones you mentioned and fixed a few others that I found like solitary currency symbol, empty string, and single space. Have a look when you get a chance. |
Thanks. Couple more edge cases I found:
What about this? /^(?!\$?\-?$)\$?\-?(0|[1-9](\d*|\d{0,2}(,\d{3})*))(\.\d{2})?$/ |
Well, two edge cases and one glaring typical case. Man, it's been a while since I've done any non-trivial regular expressions. Your version looks good. Adjusted the others accordingly, and added those test cases and other similar ones for all currencies. |
Two more: That euro one is getting quite complicated. Do they really accept the currency symbol on either side? |
Nice catch. Unfortunately they do accept it like that in certain places. I http://www.evertype.com/standards/euro/formats.html Testing in C# confirms that the euro sign can be on either end, but I 10123.25.ToString("c", new CultureInfo("el-GR")) => 10.123,25 € At this point I'm thinking it would be better to do this by On Thu, Feb 26, 2015 at 8:38 AM, chriso notifications@github.com wrote:
|
An even better alternative might be to generate the regex based on user input. Something along the lines of function currencyRegex(options) {
var symbol = '\\' + options.symbol + '?'
, negative = '\\-?'
, amount_without_sep = '[1-9]\\d*'
, amount_with_sep = '[1-9]\\d{0,2}(\\' + options.thousands + '\\d{3})*'
, valid_amounts = ['0', amount_without_sep, amount_with_sep]
, amount = '(' + valid_amounts.join('|') + ')?'
, decimal = '(\\' + options.decimal + '\\d{2})?$'
, prefix = symbol + negative;
return new RegExp(
'^' +
'(?!' + prefix + '$)' + // ensure there's an amount and/or decimal
prefix +
amount +
decimal +
'$'
);
}
validator.isCurrency = function (str, options) {
return currencyRegex(options).test(str);
}
validator.isCurrency('$10,123', {symbol: '$', thousands: ',', decimal: '.'});
// => true It's then quite easy to customize the validator. For example, you could add an option to allow whitespace, another to require a currency symbol and another to exclude negative values. You could also default each option to USD, since many locales share the same parameters. |
I like this, as long as there is no significant demand for validating The msdn article you originally referenced also points out that the https://msdn.microsoft.com/en-us/goglobal/bb688126.aspx On Thu, Feb 26, 2015 at 8:56 PM, chriso notifications@github.com wrote:
|
OK, I've implemented it in the new, customizable way. A few things to note:
UK: -£10,123.25 |
Looks great, thanks 👍 Want to clean up the commits and then I'll merge? |
OK, all set. Also updated README. |
Perfect. Thanks again. |
Available in v3.33.0. |
No problem. It was very educational. And happy to contribute to something I use all the time. Here to help if anybody brings up anything that we overlooked. |
As per suggestion, based the isCurrency validator on isMobilePhone, but used currency codes instead of locales because it seemed more reasonable, for example, to have a EUR validator that covers some slight variations instead of using all locales in the eurozone separately. Reliable information on formatting conventions seems difficult to find, so I based the RegEx's on ASP.NET/C# formatted strings with various CultureInfo instances. Added mocha tests as well.