date.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. (function($) {
  2. $.fn.bootstrapValidator.validators.date = {
  3. html5Attributes: {
  4. message: 'message',
  5. format: 'format'
  6. },
  7. /**
  8. * Return true if the input value is valid date
  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. * - message: The invalid message
  14. * - format: The date format. Default is MM/DD/YYYY
  15. * The format can be:
  16. *
  17. * i) date: Consist of DD, MM, YYYY parts which are separated by /
  18. * ii) date and time:
  19. * The time can consist of h, m, s parts which are separated by :
  20. * ii) date, time and A (indicating AM or PM)
  21. * @returns {Boolean}
  22. */
  23. validate: function(validator, $field, options) {
  24. var value = $field.val();
  25. if (value == '') {
  26. return true;
  27. }
  28. options.format = options.format || 'MM/DD/YYYY';
  29. var formats = options.format.split(' '),
  30. dateFormat = formats[0],
  31. timeFormat = (formats.length > 1) ? formats[1] : null,
  32. amOrPm = (formats.length > 2) ? formats[2] : null,
  33. sections = value.split(' '),
  34. date = sections[0],
  35. time = (sections.length > 1) ? sections[1] : null;
  36. if (formats.length != sections.length) {
  37. return false;
  38. }
  39. // Determine the separator
  40. var separator = (date.indexOf('/') != -1) ? '/' : ((date.indexOf('-') != -1) ? '-' : null);
  41. if (separator == null) {
  42. return false;
  43. }
  44. // Determine the date
  45. date = date.split(separator);
  46. dateFormat = dateFormat.split(separator);
  47. var year = date[dateFormat.indexOf('YYYY')],
  48. month = date[dateFormat.indexOf('MM')],
  49. day = date[dateFormat.indexOf('DD')];
  50. // Determine the time
  51. var minutes = null, hours = null, seconds = null;
  52. if (timeFormat) {
  53. timeFormat = timeFormat.split(':'),
  54. time = time.split(':');
  55. if (timeFormat.length != time.length) {
  56. return false;
  57. }
  58. hours = time.length > 0 ? time[0] : null;
  59. minutes = time.length > 1 ? time[1] : null;
  60. seconds = time.length > 2 ? time[2] : null;
  61. // Validate seconds
  62. if (seconds) {
  63. seconds = parseInt(seconds, 10);
  64. if (seconds < 0 || seconds > 60) {
  65. return false;
  66. }
  67. }
  68. // Validate hours
  69. if (hours) {
  70. hours = parseInt(hours, 10);
  71. if (hours < 0 || hours >= 24 || (amOrPm && hours > 12)) {
  72. return false;
  73. }
  74. }
  75. // Validate minutes
  76. if (minutes) {
  77. minutes = parseInt(minutes, 10);
  78. if (minutes < 0 || minutes > 59) {
  79. return false;
  80. }
  81. }
  82. }
  83. // Validate day, month, and year
  84. day = parseInt(day, 10);
  85. month = parseInt(month, 10);
  86. year = parseInt(year, 10);
  87. if (year < 1000 || year > 9999 || month == 0 || month > 12) {
  88. return false;
  89. }
  90. var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
  91. // Update the number of days in Feb of leap year
  92. if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
  93. numDays[1] = 29;
  94. }
  95. // Check the day
  96. return (day > 0 && day <= numDays[month - 1]);
  97. }
  98. };
  99. }(window.jQuery));