creditCard.js 3.7 KB

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