date.js 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. describe('date', function() {
  2. beforeEach(function() {
  3. $([
  4. '<form class="form-horizontal" id="dateForm">',
  5. '<div id="msg"></div>',
  6. '<div class="form-group">',
  7. '<input type="text" name="date" data-bv-date />',
  8. '</div>',
  9. '</form>'
  10. ].join('\n')).appendTo('body');
  11. $('#dateForm').bootstrapValidator();
  12. this.bv = $('#dateForm').data('bootstrapValidator');
  13. this.$date = this.bv.getFieldElements('date');
  14. });
  15. afterEach(function() {
  16. $('#dateForm').bootstrapValidator('destroy').remove();
  17. });
  18. it('YYYY/MM/DD', function() {
  19. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
  20. this.$date.val('2000/01/30');
  21. this.bv.validate();
  22. expect(this.bv.isValid()).toBeTruthy();
  23. // Invalid year
  24. this.bv.resetForm();
  25. this.$date.val('100/10/20');
  26. this.bv.validate();
  27. expect(this.bv.isValid()).toEqual(false);
  28. // Invalid month
  29. this.bv.resetForm();
  30. this.$date.val('2000/00/10');
  31. this.bv.validate();
  32. expect(this.bv.isValid()).toEqual(false);
  33. this.bv.resetForm();
  34. this.$date.val('2000/15/10');
  35. this.bv.validate();
  36. expect(this.bv.isValid()).toEqual(false);
  37. // Invalid day
  38. this.bv.resetForm();
  39. this.$date.val('2000/03/00');
  40. this.bv.validate();
  41. expect(this.bv.isValid()).toEqual(false);
  42. this.bv.resetForm();
  43. this.$date.val('2000/10/32');
  44. this.bv.validate();
  45. expect(this.bv.isValid()).toEqual(false);
  46. // Negative number
  47. this.bv.resetForm();
  48. this.$date.val('-2000/10/20');
  49. this.bv.validate();
  50. expect(this.bv.isValid()).toEqual(false);
  51. this.bv.resetForm();
  52. this.$date.val('2000/-10/20');
  53. this.bv.validate();
  54. expect(this.bv.isValid()).toEqual(false);
  55. this.bv.resetForm();
  56. this.$date.val('2000/10/-20');
  57. this.bv.validate();
  58. expect(this.bv.isValid()).toEqual(false);
  59. // Consist invalid characters
  60. // Issue #310
  61. this.bv.resetForm();
  62. this.$date.val('aaaa/');
  63. this.bv.validate();
  64. expect(this.bv.isValid()).toEqual(false);
  65. this.bv.resetForm();
  66. this.$date.val('2004df/1dd1/5ffg');
  67. this.bv.validate();
  68. expect(this.bv.isValid()).toEqual(false);
  69. // Issue #475
  70. this.bv.resetForm();
  71. this.$date.val('2014/09');
  72. this.bv.validate();
  73. expect(this.bv.isValid()).toEqual(false);
  74. this.bv.resetForm();
  75. this.$date.val('2014/09/');
  76. this.bv.validate();
  77. expect(this.bv.isValid()).toEqual(false);
  78. this.bv.resetForm();
  79. this.$date.val('2014//15');
  80. this.bv.validate();
  81. expect(this.bv.isValid()).toEqual(false);
  82. this.bv.resetForm();
  83. this.$date.val('/09/15');
  84. this.bv.validate();
  85. expect(this.bv.isValid()).toEqual(false);
  86. });
  87. it('MM/DD/YYYY', function() {
  88. this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
  89. this.$date.val('09/15/2020');
  90. this.bv.validate();
  91. expect(this.bv.isValid()).toBeTruthy();
  92. this.bv.resetForm();
  93. this.$date.val('09/15');
  94. this.bv.validate();
  95. expect(this.bv.isValid()).toEqual(false);
  96. this.bv.resetForm();
  97. this.$date.val('09/15/');
  98. this.bv.validate();
  99. expect(this.bv.isValid()).toEqual(false);
  100. });
  101. it('number of days in February', function() {
  102. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD');
  103. this.$date.val('2000/02/28');
  104. this.bv.validate();
  105. expect(this.bv.isValid()).toBeTruthy();
  106. this.bv.resetForm();
  107. this.$date.val('2000/02/29');
  108. this.bv.validate();
  109. expect(this.bv.isValid()).toBeTruthy();
  110. this.bv.resetForm();
  111. this.$date.val('2001/02/29');
  112. this.bv.validate();
  113. expect(this.bv.isValid()).toEqual(false);
  114. });
  115. // Issue #681
  116. it('date, month, year are prefixed by zero', function() {
  117. this.bv.updateOption('date', 'date', 'format', 'MM/DD/YYYY');
  118. this.$date.val('0012/08/2014');
  119. this.bv.validate();
  120. expect(this.bv.isValid()).toEqual(false);
  121. this.bv.resetForm();
  122. this.$date.val('12/0008/2014');
  123. this.bv.validate();
  124. expect(this.bv.isValid()).toEqual(false);
  125. this.bv.resetForm();
  126. this.$date.val('12/08/002014');
  127. this.bv.validate();
  128. expect(this.bv.isValid()).toEqual(false);
  129. this.bv.resetForm();
  130. this.$date.val('12/08/2014');
  131. this.bv.validate();
  132. expect(this.bv.isValid()).toBeTruthy();
  133. });
  134. it('hours, minutes, seconds are prefixed by zero', function() {
  135. this.bv.updateOption('date', 'date', 'format', 'YYYY/MM/DD h:m:s');
  136. this.$date.val('2014/08/17 0007:30:00');
  137. this.bv.validate();
  138. expect(this.bv.isValid()).toEqual(false);
  139. this.bv.resetForm();
  140. this.$date.val('2014/08/17 07:030:00');
  141. this.bv.validate();
  142. expect(this.bv.isValid()).toEqual(false);
  143. this.bv.resetForm();
  144. this.$date.val('2014/08/17 07:30:0000');
  145. this.bv.validate();
  146. expect(this.bv.isValid()).toEqual(false);
  147. this.bv.resetForm();
  148. this.$date.val('2014/08/17 07:30:00');
  149. this.bv.validate();
  150. expect(this.bv.isValid()).toBeTruthy();
  151. });
  152. });