Browse Source

Add $.fn.bootstrapValidator.helpers.date for validating a date, re-used in date validator

phuoc 11 years ago
parent
commit
4c3cab3461
3 changed files with 24 additions and 12 deletions
  1. 1 0
      CHANGELOG.md
  2. 22 0
      src/js/bootstrapValidator.js
  3. 1 12
      src/js/validator/date.js

+ 1 - 0
CHANGELOG.md

@@ -2,6 +2,7 @@
 
 ## v0.4.5 (not released yet)
 
+* Add ```$.fn.bootstrapValidator.helpers.date``` for validating a date, re-used in ```date```, ```id``` validators
 * [#233](https://github.com/nghuuphuoc/bootstrapvalidator/issues/233): Add ```threshold``` option
 * [#232](https://github.com/nghuuphuoc/bootstrapvalidator/issues/232): Add ```id``` validator
 * When parsing options from HTML attributes, don't add the field which hasn't validators. It improves fixes for [#191](https://github.com/nghuuphuoc/bootstrapvalidator/issues/191), [#223](https://github.com/nghuuphuoc/bootstrapvalidator/issues/223)

+ 22 - 0
src/js/bootstrapValidator.js

@@ -851,6 +851,28 @@
     // Helper methods, which can be used in validator class
     $.fn.bootstrapValidator.helpers = {
         /**
+         * Validate a date
+         *
+         * @param {Number} year The full year in 4 digits
+         * @param {Number} month The month number
+         * @param {Number} day The day number
+         * @returns {Boolean}
+         */
+        date: function(year, month, day) {
+            if (year < 1000 || year > 9999 || month == 0 || month > 12) {
+                return false;
+            }
+            var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
+            // Update the number of days in Feb of leap year
+            if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
+                numDays[1] = 29;
+            }
+
+            // Check the day
+            return (day > 0 && day <= numDays[month - 1]);
+        },
+
+        /**
          * Implement Luhn validation algorithm ((http://en.wikipedia.org/wiki/Luhn))
          * Credit to https://gist.github.com/ShirtlessKirk/2134376
          *

+ 1 - 12
src/js/validator/date.js

@@ -98,18 +98,7 @@
             month = parseInt(month, 10);
             year  = parseInt(year, 10);
 
-            if (year < 1000 || year > 9999 || month == 0 || month > 12) {
-                return false;
-            }
-
-            var numDays = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
-            // Update the number of days in Feb of leap year
-            if (year % 400 == 0 || (year % 100 != 0 && year % 4 == 0)) {
-                numDays[1] = 29;
-            }
-
-            // Check the day
-            return (day > 0 && day <= numDays[month - 1]);
+            return $.fn.bootstrapValidator.helpers.date(year, month, day);
         }
     };
 }(window.jQuery));