Skip to content

Commit

Permalink
Merge 3a19a0c into bab1c14
Browse files Browse the repository at this point in the history
  • Loading branch information
regularmike committed Feb 24, 2015
2 parents bab1c14 + 3a19a0c commit d3ff838
Show file tree
Hide file tree
Showing 3 changed files with 198 additions and 1 deletion.
181 changes: 181 additions & 0 deletions test/validators.js
Original file line number Diff line number Diff line change
Expand Up @@ -1163,5 +1163,186 @@ describe('Validators', function () {
, args: ['el-GR']
});
});

it('should validate currency', function() {
test({
validator: 'isCurrency'
, valid: [
'123,456.78',
'$6,954,231',
'$10.03',
'10.03',
'1.39',
'.03',
'0.10',
'$10567.01',
'$0.01',
'$1,234,567.89'
]
, invalid: [
'1.234',
'$1.1',
'50#.50',
'123.@$',
'$$500',
'.0001',
'$.001',
'$0.001',
'12,34.56',
'123456,123,123456'
],
args: ['USD']
});

test({
validator: 'isCurrency'
, valid: [
'123,456.78',
'¥6,954,231',
'¥10.03',
'10.03',
'1.39',
'.03',
'0.10',
'¥10567.01',
'¥0.01',
'¥1,234,567.89'
]
, invalid: [
'1.234',
'¥1.1',
'50#.50',
'123.@$',
'¥¥500',
'.0001',
'¥.001',
'¥0.001',
'12,34.56',
'123456,123,123456'
],
args: ['CNY']
});

test({
validator: 'isCurrency'
, valid: [
'123 456,78',
'R 6 954 231',
'R10,03',
'10,03',
'1,39',
',03',
'0,10',
'R10567,01',
'R0,01',
'R1 234 567,89'
]
, invalid: [
'1,234',
'R,1',
'50#,50',
'123,@R',
'RR500',
',0001',
'R,001',
'R0,001',
'12 34,56',
'123456 123 123456'
],
args: ['ZAR']
});

test({
validator: 'isCurrency'
, valid: [
'123,456.78',
'$6,954,231',
'$10.03',
'10.03',
'1.39',
'.03',
'0.10',
'$10567.01',
'$0.01',
'$1,234,567.89'
]
, invalid: [
'1.234',
'$1.1',
'50#.50',
'123.@$',
'$$500',
'.0001',
'$.001',
'$0.001',
'12,34.56',
'123456,123,123456'
],
args: ['AUD']
});

test({
validator: 'isCurrency'
, valid: [
'123,456.78',
'$6,954,231',
'$10.03',
'10.03',
'1.39',
'.03',
'0.10',
'$10567.01',
'$0.01',
'$1,234,567.89'
]
, invalid: [
'1.234',
'$1.1',
'50#.50',
'123.@$',
'$$500',
'.0001',
'$.001',
'$0.001',
'12,34.56',
'123456,123,123456'
],
args: ['HKD']
});

test({
validator: 'isCurrency'
, valid: [
'123 456,78',
'123.456,78',
'€6.954.231',
'€ 896.954.231',
'16.954.231 €',
'12 346 954 231€',
'€10,03',
'10,03',
'1,39',
',03',
'0,10',
'€10567,01',
'€ 0,01',
'€1 234 567,89',
'€1.234.567,89'
]
, invalid: [
'1,234',
'€ 1,1',
'50#,50',
'123,@€ ',
'€€500',
',0001',
'€ ,001',
'€0,001',
'12.34,56',
'123456.123.123456'
],
args: ['EUR']
});
});

});
16 changes: 16 additions & 0 deletions validator.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,15 @@
'pt-PT': /^(\+351)?9[1236]\d{7}$/,
'el-GR' : /^(\+30)?((2\d{9})|(69\d{8}))$/
};

var currencies = {
'USD': /^\$?\-?([0-9]{1,3})?((,[0-9]{3})*|([0-9]{3})*)(\.[0-9]{2})?$/,
'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})?$/,
'EUR': /^\-?(€|€ )?([0-9]{1,3})?(( [0-9]{3})*|(\.[0-9]{3})*|([0-9]{3})*)(,[0-9]{2})?(€| €)?$/
};

validator.extend = function (name, fn) {
validator[name] = function () {
Expand Down Expand Up @@ -465,6 +474,13 @@
}
return false;
};

validator.isCurrency = function(str, code) {
if (code in currencies) {
return currencies[code].test(str);
}
return false;
}

validator.isJSON = function (str) {
try {
Expand Down

0 comments on commit d3ff838

Please sign in to comment.