creditCard.js 3.3 KB

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