cvv.js 4.3 KB

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