2150 |
mathias |
1 |
if(!dojo._hasResource["dojox.validate.creditCard"]){ //_hasResource checks added by build. Do not use _hasResource directly in your code.
|
|
|
2 |
dojo._hasResource["dojox.validate.creditCard"] = true;
|
|
|
3 |
dojo.provide("dojox.validate.creditCard");
|
|
|
4 |
|
|
|
5 |
dojo.require("dojox.validate._base");
|
|
|
6 |
|
|
|
7 |
/*
|
|
|
8 |
Validates Credit Cards using account number rules in conjunction with the Luhn algorigthm
|
|
|
9 |
|
|
|
10 |
*/
|
|
|
11 |
|
|
|
12 |
dojox.validate.isValidCreditCard = function(/*String|Int*/value, /*String*/ccType){
|
|
|
13 |
//Summary:
|
|
|
14 |
// checks if type matches the # scheme, and if Luhn checksum is accurate (unless its an Enroute card, the checkSum is skipped)
|
|
|
15 |
|
|
|
16 |
//Value: Boolean
|
|
|
17 |
if(value&&ccType&&((ccType.toLowerCase()=='er'||dojox.validate.isValidLuhn(value))&&(dojox.validate.isValidCreditCardNumber(value,ccType.toLowerCase())))){
|
|
|
18 |
return true; //Boolean
|
|
|
19 |
}
|
|
|
20 |
return false; //Boolean
|
|
|
21 |
}
|
|
|
22 |
dojox.validate.isValidCreditCardNumber = function(/*String|Int*/value,/*String?*/ccType) {
|
|
|
23 |
//Summary:
|
|
|
24 |
// checks if the # matches the pattern for that card or any card types if none is specified
|
|
|
25 |
// value == CC #, white spaces and dashes are ignored
|
|
|
26 |
// ccType is of the values in cardinfo -- if Omitted it it returns a | delimited string of matching card types, or false if no matches found
|
|
|
27 |
|
|
|
28 |
//Value: Boolean
|
|
|
29 |
|
|
|
30 |
if(typeof value!='string'){
|
|
|
31 |
value = String(value);
|
|
|
32 |
}
|
|
|
33 |
value = value.replace(/[- ]/g,''); //ignore dashes and whitespaces
|
|
|
34 |
/* FIXME: not sure on all the abbreviations for credit cards,below is what each stands for atleast to my knowledge
|
|
|
35 |
mc: Mastercard
|
|
|
36 |
ec: Eurocard
|
|
|
37 |
vi: Visa
|
|
|
38 |
ax: American Express
|
|
|
39 |
dc: Diners Club
|
|
|
40 |
bl: Carte Blanch
|
|
|
41 |
di: Discover
|
|
|
42 |
jcb: JCB
|
|
|
43 |
er: Enroute
|
|
|
44 |
*/
|
|
|
45 |
var results=[];
|
|
|
46 |
var cardinfo = {
|
|
|
47 |
'mc':'5[1-5][0-9]{14}','ec':'5[1-5][0-9]{14}','vi':'4([0-9]{12}|[0-9]{15})',
|
|
|
48 |
'ax':'3[47][0-9]{13}', 'dc':'3(0[0-5][0-9]{11}|[68][0-9]{12})',
|
|
|
49 |
'bl':'3(0[0-5][0-9]{11}|[68][0-9]{12})','di':'6011[0-9]{12}',
|
|
|
50 |
'jcb':'(3[0-9]{15}|(2131|1800)[0-9]{11})','er':'2(014|149)[0-9]{11}'
|
|
|
51 |
};
|
|
|
52 |
if(ccType&&dojo.indexOf(cardinfo,ccType.toLowerCase())){
|
|
|
53 |
return Boolean(value.match(cardinfo[ccType.toLowerCase()])); // boolean
|
|
|
54 |
}else{
|
|
|
55 |
for(var p in cardinfo){
|
|
|
56 |
if(value.match('^'+cardinfo[p]+'$')!=null){
|
|
|
57 |
results.push(p);
|
|
|
58 |
}
|
|
|
59 |
}
|
|
|
60 |
return (results.length)?results.join('|'):false; // string | boolean
|
|
|
61 |
}
|
|
|
62 |
}
|
|
|
63 |
|
|
|
64 |
dojox.validate.isValidCvv = function(/*String|Int*/value, /*String*/ccType) {
|
|
|
65 |
//Summary:
|
|
|
66 |
// returns true if the security code (CCV) matches the correct format for supplied ccType
|
|
|
67 |
|
|
|
68 |
//Value: Boolean
|
|
|
69 |
|
|
|
70 |
if(typeof value!='string'){
|
|
|
71 |
value=String(value);
|
|
|
72 |
}
|
|
|
73 |
var format;
|
|
|
74 |
switch (ccType.toLowerCase()){
|
|
|
75 |
case 'mc':
|
|
|
76 |
case 'ec':
|
|
|
77 |
case 'vi':
|
|
|
78 |
case 'di':
|
|
|
79 |
format = '###';
|
|
|
80 |
break;
|
|
|
81 |
case 'ax':
|
|
|
82 |
format = '####';
|
|
|
83 |
break;
|
|
|
84 |
default:
|
|
|
85 |
return false; //Boolean
|
|
|
86 |
}
|
|
|
87 |
var flags = {format:format};
|
|
|
88 |
//FIXME? Why does isNumberFormat take an object for flags when its only parameter is either a string or an array inside the object?
|
|
|
89 |
if ((value.length == format.length)&&(dojox.validate.isNumberFormat(value, flags))){
|
|
|
90 |
return true; //Boolean
|
|
|
91 |
}
|
|
|
92 |
return false; //Boolean
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
}
|