creditCard.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. describe('creditCard', function() {
  2. // Get the fake credit card number at http://www.getcreditcardnumbers.com/
  3. beforeEach(function() {
  4. var html = [
  5. '<div class="container">',
  6. '<form class="form-horizontal" id="ccForm">',
  7. '<div class="form-group">',
  8. '<input type="text" name="cc" data-bv-creditcard />',
  9. '</div>',
  10. '</form>',
  11. '</div>'
  12. ].join('\n');
  13. $(html).appendTo('body');
  14. $('#ccForm').bootstrapValidator();
  15. this.bv = $('#ccForm').data('bootstrapValidator');
  16. this.$creditCard = this.bv.getFieldElements('cc');
  17. });
  18. afterEach(function() {
  19. $('#ccForm').bootstrapValidator('destroy').parent().remove();
  20. });
  21. it('accept spaces', function() {
  22. this.$creditCard.val('5267 9789 9451 9654');
  23. this.bv.validate();
  24. expect(this.bv.isValidField('cc')).toBeTruthy();
  25. });
  26. it('accept dashes', function() {
  27. this.$creditCard.val('6011-2649-6840-4521');
  28. this.bv.validate();
  29. expect(this.bv.isValidField('cc')).toBeTruthy();
  30. });
  31. it('invalid format', function() {
  32. this.$creditCard.val('4539.1870.2954.3862');
  33. this.bv.validate();
  34. expect(this.bv.isValidField('cc')).toEqual(false);
  35. });
  36. it('American Express', function() {
  37. this.$creditCard.val('340653705597107');
  38. this.bv.validate();
  39. expect(this.bv.isValidField('cc')).toBeTruthy();
  40. });
  41. it('American Express invalid length', function() {
  42. this.$creditCard.val('3744148309166730');
  43. this.bv.validate();
  44. expect(this.bv.isValidField('cc')).toEqual(false);
  45. });
  46. it('American Express invalid prefix', function() {
  47. this.$creditCard.val('356120148436654');
  48. this.bv.validate();
  49. expect(this.bv.isValidField('cc')).toEqual(false);
  50. });
  51. it('Diners Club', function() {
  52. this.$creditCard.val('30130708434187');
  53. this.bv.validate();
  54. expect(this.bv.isValidField('cc')).toBeTruthy();
  55. });
  56. it('Diners Club (US)', function() {
  57. this.$creditCard.val('5517479515603901');
  58. this.bv.validate();
  59. expect(this.bv.isValidField('cc')).toBeTruthy();
  60. });
  61. it('Discover', function() {
  62. this.$creditCard.val('6011734674929094');
  63. this.bv.validate();
  64. expect(this.bv.isValidField('cc')).toBeTruthy();
  65. });
  66. it('JCB', function() {
  67. this.$creditCard.val('3566002020360505');
  68. this.bv.validate();
  69. expect(this.bv.isValidField('cc')).toBeTruthy();
  70. });
  71. it('Laser', function() {
  72. this.$creditCard.val('6304 9000 1774 0292 441');
  73. this.bv.validate();
  74. expect(this.bv.isValidField('cc')).toBeTruthy();
  75. });
  76. it('Maestro', function() {
  77. this.$creditCard.val('6762835098779303');
  78. this.bv.validate();
  79. expect(this.bv.isValidField('cc')).toBeTruthy();
  80. });
  81. it('Mastercard', function() {
  82. this.$creditCard.val('5303765013600904');
  83. this.bv.validate();
  84. expect(this.bv.isValidField('cc')).toBeTruthy();
  85. });
  86. it('Solo', function() {
  87. this.$creditCard.val('6334580500000000');
  88. this.bv.validate();
  89. expect(this.bv.isValidField('cc')).toBeTruthy();
  90. });
  91. it('Visa', function() {
  92. this.$creditCard.val('4929248980295542');
  93. this.bv.validate();
  94. expect(this.bv.isValidField('cc')).toBeTruthy();
  95. });
  96. it('Visa invalid check digit', function() {
  97. this.$creditCard.val('4532599916257826');
  98. this.bv.validate();
  99. expect(this.bv.isValidField('cc')).toEqual(false);
  100. });
  101. });