source: Dev/branches/rest-dojo-ui/client/dojox/validate/creditCard.js @ 256

Last change on this file since 256 was 256, checked in by hendrikvanantwerpen, 13 years ago

Reworked project structure based on REST interaction and Dojo library. As
soon as this is stable, the old jQueryUI branch can be removed (it's
kept for reference).

File size: 3.3 KB
Line 
1define(["dojo/_base/lang", "./_base"], function(lang, validate){
2/*=====
3
4        dojox.validate.creditCard = {
5                // summary:
6                //              Module provides validation functions for Credit Cards, using account number
7                //              rules in conjunction with the Luhn algorigthm, with a plugable card info database.
8        };
9
10        validate = dojox.validate;
11
12=====*/
13
14validate._cardInfo = {
15        // summary: A dictionary list of credit card abbreviations
16        //
17        // description:
18        //
19        //              A hash of valid CC abbreviations and regular expressions
20        //
21        //              mc: Mastercard
22        //              ec: Eurocard
23        //              vi: Visa
24        //              ax: American Express
25        //              dc: Diners Club
26        //              bl: Carte Blanch
27        //              di: Discover
28        //              jcb: JCB
29        //              er: Enroute
30        //
31        //      example:
32        //              Define your own card, gift-card, whatever. Starts with 7,
33        //              is 15 total length.
34        //      | dojo.mixin(dojox.validate._cardInfo, {
35        //      |       "my":"7[0-9]{14}"
36        //      | });
37       
38        'mc':'5[1-5][0-9]{14}',
39        'ec':'5[1-5][0-9]{14}',
40        'vi':'4(?:[0-9]{12}|[0-9]{15})',
41        'ax':'3[47][0-9]{13}',
42        'dc':'3(?:0[0-5][0-9]{11}|[68][0-9]{12})',
43        'bl':'3(?:0[0-5][0-9]{11}|[68][0-9]{12})',
44        'di':'6011[0-9]{12}',
45        'jcb':'(?:3[0-9]{15}|(2131|1800)[0-9]{11})',
46        'er':'2(?:014|149)[0-9]{11}'
47};
48
49validate.isValidCreditCard = function(value, ccType){
50        // summary: Validate a credit card number by type with Luhn checking.
51        //
52        // description:
53        //              Checks if a credit card type matches the # scheme in a passed value, and if
54        //              the Luhn checksum is accurate (unless its an Enroute card, in which case
55        //              the checkSum is skipped), returning a Boolean to check against.
56        //
57        // value: String|Int
58        //              A Value (credit card number) to validate
59        //
60        // ccType: String
61        //              A credit-card abbreviation.
62        //
63        // example:
64        // |    if(dojox.validate.isValidCreditCard("12345", "mc")){
65        // |            console.log('inconceivable');
66        // |    }
67       
68        return ((ccType.toLowerCase() == 'er' || validate.isValidLuhn(value)) &&
69                        validate.isValidCreditCardNumber(value, ccType.toLowerCase())); // Boolean
70};
71
72validate.isValidCreditCardNumber = function(value, ccType){
73        // summary:
74        //              Checks if value matches the pattern for that card or any card types if none is specified
75        //
76        // value: String|Int
77        //              CC #, white spaces and dashes are ignored
78        //
79        // ccType: String?
80        //              One of the abbreviation values in `dojox.validate._cardInfo` --
81        //              if Omitted, function returns a `|` delimited string of matching card types,
82        //              or false if no matches found.
83
84        value = String(value).replace(/[- ]/g,''); //ignore dashes and whitespaces
85
86        var cardinfo = validate._cardInfo, results = [];
87        if(ccType){
88                var expr = '^' + cardinfo[ccType.toLowerCase()] + '$';
89                return expr ? !!value.match(expr) : false; // boolean
90        }
91
92        for(var p in cardinfo){
93                if(value.match('^' + cardinfo[p] + '$')){
94                        results.push(p);
95                }
96        }
97        return results.length ? results.join('|') : false; // String | boolean
98};
99
100validate.isValidCvv = function(/* String|Int */value, /* String */ccType) {
101        // summary:
102        //      Validate the security code (CCV) for a passed credit-card type.
103        //
104        // description:
105        //
106        // value:
107       
108        if(!lang.isString(value)){
109                value = String(value);
110        }
111        var format;
112        switch (ccType.toLowerCase()){
113                case 'mc':
114                case 'ec':
115                case 'vi':
116                case 'di':
117                        format = '###';
118                        break;
119                case 'ax':
120                        format = '####';
121                        break;
122        }
123       
124        return !!format && value.length && validate.isNumberFormat(value, { format: format }); // Boolean
125};
126
127return validate;
128});
Note: See TracBrowser for help on using the repository browser.