creditCard.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. (function($) {
  2. $.fn.bootstrapValidator.i18n.creditCard = $.extend($.fn.bootstrapValidator.i18n.creditCard || {}, {
  3. 'default': 'Please enter a valid credit card number'
  4. });
  5. $.fn.bootstrapValidator.validators.creditCard = {
  6. /**
  7. * Return true if the input value is valid credit card number
  8. * Based on https://gist.github.com/DiegoSalazar/4075533
  9. *
  10. * @param {BootstrapValidator} validator The validator plugin instance
  11. * @param {jQuery} $field Field element
  12. * @param {Object} [options] Can consist of the following key:
  13. * - message: The invalid message
  14. * @returns {Boolean}
  15. */
  16. validate: function(validator, $field, options) {
  17. var value = $field.val();
  18. if (value === '') {
  19. return true;
  20. }
  21. // Accept only digits, dashes or spaces
  22. if (/[^0-9-\s]+/.test(value)) {
  23. return false;
  24. }
  25. value = value.replace(/\D/g, '');
  26. if (!$.fn.bootstrapValidator.helpers.luhn(value)) {
  27. return false;
  28. }
  29. // Validate the card number based on prefix (IIN ranges) and length
  30. var cards = {
  31. AMERICAN_EXPRESS: {
  32. length: [15],
  33. prefix: ['34', '37']
  34. },
  35. DINERS_CLUB: {
  36. length: [14],
  37. prefix: ['300', '301', '302', '303', '304', '305', '36']
  38. },
  39. DINERS_CLUB_US: {
  40. length: [16],
  41. prefix: ['54', '55']
  42. },
  43. DISCOVER: {
  44. length: [16],
  45. prefix: ['6011', '622126', '622127', '622128', '622129', '62213',
  46. '62214', '62215', '62216', '62217', '62218', '62219',
  47. '6222', '6223', '6224', '6225', '6226', '6227', '6228',
  48. '62290', '62291', '622920', '622921', '622922', '622923',
  49. '622924', '622925', '644', '645', '646', '647', '648',
  50. '649', '65']
  51. },
  52. JCB: {
  53. length: [16],
  54. prefix: ['3528', '3529', '353', '354', '355', '356', '357', '358']
  55. },
  56. LASER: {
  57. length: [16, 17, 18, 19],
  58. prefix: ['6304', '6706', '6771', '6709']
  59. },
  60. MAESTRO: {
  61. length: [12, 13, 14, 15, 16, 17, 18, 19],
  62. prefix: ['5018', '5020', '5038', '6304', '6759', '6761', '6762', '6763', '6764', '6765', '6766']
  63. },
  64. MASTERCARD: {
  65. length: [16],
  66. prefix: ['51', '52', '53', '54', '55']
  67. },
  68. SOLO: {
  69. length: [16, 18, 19],
  70. prefix: ['6334', '6767']
  71. },
  72. UNIONPAY: {
  73. length: [16, 17, 18, 19],
  74. prefix: ['622126', '622127', '622128', '622129', '62213', '62214',
  75. '62215', '62216', '62217', '62218', '62219', '6222', '6223',
  76. '6224', '6225', '6226', '6227', '6228', '62290', '62291',
  77. '622920', '622921', '622922', '622923', '622924', '622925']
  78. },
  79. VISA: {
  80. length: [16],
  81. prefix: ['4']
  82. }
  83. };
  84. var type, i;
  85. for (type in cards) {
  86. for (i in cards[type].prefix) {
  87. if (value.substr(0, cards[type].prefix[i].length) === cards[type].prefix[i] // Check the prefix
  88. && $.inArray(value.length, cards[type].length) !== -1) // and length
  89. {
  90. return true;
  91. }
  92. }
  93. }
  94. return false;
  95. }
  96. };
  97. }(window.jQuery));