Skip to content

Commit 1a981b4

Browse files
James Thompsonjzaefferer
authored andcommitted
Methods: Add additional method for currency
Closes gh-701
1 parent a864211 commit 1a981b4

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/additional/currency.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/**
2+
* Validates currencies with any given symbols by @jameslouiz
3+
* Symbols can be optional or required. Symbols required by default
4+
*
5+
* Usage examples:
6+
* currency: ['£', false] - Use false for soft currency validation
7+
* currency: ['$', false]
8+
* currency: ['RM', false] - also works with text based symbols such as 'RM' - Malaysia Ringgit etc
9+
*
10+
* <input class="currencyInput" name="currencyInput">
11+
*
12+
* Soft symbol checking
13+
* currencyInput: {
14+
* currency: ['$', false]
15+
* }
16+
*
17+
* Strict symbol checking (default)
18+
* currencyInput: {
19+
* currency: '$'
20+
* //OR
21+
* currency: ['$', true]
22+
* }
23+
*
24+
* Multiple Symbols
25+
* currencyInput: {
26+
* currency: '$,£,¢'
27+
* }
28+
*/
29+
jQuery.validator.addMethod('currency', function(value, element, param) {
30+
var paramType = typeof param === 'string',
31+
symbol = paramType ? param : param[0],
32+
soft = paramType ? true : param[1],
33+
regex;
34+
35+
symbol = symbol.replace(/,/g, '');
36+
symbol = soft ? symbol + ']' : symbol + ']?';
37+
regex = '^[' + symbol + '([1-9]{1}[0-9]{0,2}(\\,[0-9]{3})*(\\.[0-9]{0,2})?|[1-9]{1}[0-9]{0,}(\\.[0-9]{0,2})?|0(\\.[0-9]{0,2})?|(\\.[0-9]{1,2})?)$';
38+
regex = new RegExp(regex);
39+
return this.optional(element) || regex.test(value);
40+
41+
}, 'Please specify a valid currency');

test/methods.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1166,4 +1166,28 @@ test("rangeWords", function(){
11661166
ok(!method( "<div>But you can “count” me as too long</div>", rangeWords), "Too many words with smartquotes w/ HTML");
11671167
});
11681168

1169+
test("currency", function() { // Works with any symbol
1170+
var method = methodTest( "currency" );
1171+
ok( method( "£9", '£'), "Valid currency" );
1172+
ok( method( "£9.9", '£'), "Valid currency" );
1173+
ok( method( "£9.99", '£'), "Valid currency" );
1174+
ok( method( "£9.90", '£'), "Valid currency" );
1175+
ok( method( "£9,999.9", '£'), "Valid currency" );
1176+
ok( method( "£9,999.99", '£'), "Valid currency" );
1177+
ok( method( "£9,999,999.9", '£'), "Valid currency" );
1178+
ok( method( "9", ['£', false]), "Valid currency" );
1179+
ok( method( "9.9", ['£', false]), "Valid currency" );
1180+
ok( method( "9.99", ['£', false]), "Valid currency" );
1181+
ok( method( "9.90", ['£', false]), "Valid currency" );
1182+
ok( method( "9,999.9", ['£', false]), "Valid currency" );
1183+
ok( method( "9,999.99", ['£', false]), "Valid currency" );
1184+
ok( method( "9,999,999.9", ['£', false]), "Valid currency" );
1185+
ok(!method( "9,", '£'), "Invalid currency" );
1186+
ok(!method( "9,99.99", '£'), "Invalid currency" );
1187+
ok(!method( "9,", '£'), "Invalid currency" );
1188+
ok(!method( "9.999", '£'), "Invalid currency" );
1189+
ok(!method( "9.999", '£'), "Invalid currency" );
1190+
ok(!method( "9.99,9", '£'), "Invalid currency" );
1191+
});
1192+
11691193
})(jQuery);

0 commit comments

Comments
 (0)