Browse Source

#232: Validate South African ID

nghuuphuoc 11 years ago
parent
commit
5c3a400589
3 changed files with 57 additions and 1 deletions
  1. 28 0
      dist/js/bootstrapValidator.js
  2. 1 1
      dist/js/bootstrapValidator.min.js
  3. 28 0
      src/js/validator/id.js

+ 28 - 0
dist/js/bootstrapValidator.js

@@ -2365,6 +2365,34 @@
          */
         _sm: function(value) {
             return /^\d{5}$/.test(value);
+        },
+
+        /**
+         * Validate South African ID
+         * Example:
+         * - Valid: 8001015009087
+         * - Invalid: 8001015009287, 8001015009086
+         *
+         * @see http://en.wikipedia.org/wiki/National_identification_number#South_Africa
+         * @param {String} value The ID
+         * @returns {Boolean}
+         */
+        _za: function(value) {
+            if (!/^[0-9]{10}[0|1][8|9][0-9]$/.test(value)) {
+                return false;
+            }
+            var year        = parseInt(value.substr(0, 2)),
+                currentYear = new Date().getFullYear() % 100,
+                month       = parseInt(value.substr(2, 2)),
+                day         = parseInt(value.substr(4, 2));
+            year = (year >= currentYear) ? (year + 1900) : (year + 2000);
+
+            if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
+                return false;
+            }
+
+            // Validate the last check digit
+            return $.fn.bootstrapValidator.helpers.luhn(value);
         }
     };
 }(window.jQuery));

File diff suppressed because it is too large
+ 1 - 1
dist/js/bootstrapValidator.min.js


+ 28 - 0
src/js/validator/id.js

@@ -541,6 +541,34 @@
          */
         _sm: function(value) {
             return /^\d{5}$/.test(value);
+        },
+
+        /**
+         * Validate South African ID
+         * Example:
+         * - Valid: 8001015009087
+         * - Invalid: 8001015009287, 8001015009086
+         *
+         * @see http://en.wikipedia.org/wiki/National_identification_number#South_Africa
+         * @param {String} value The ID
+         * @returns {Boolean}
+         */
+        _za: function(value) {
+            if (!/^[0-9]{10}[0|1][8|9][0-9]$/.test(value)) {
+                return false;
+            }
+            var year        = parseInt(value.substr(0, 2)),
+                currentYear = new Date().getFullYear() % 100,
+                month       = parseInt(value.substr(2, 2)),
+                day         = parseInt(value.substr(4, 2));
+            year = (year >= currentYear) ? (year + 1900) : (year + 2000);
+
+            if (!$.fn.bootstrapValidator.helpers.date(year, month, day)) {
+                return false;
+            }
+
+            // Validate the last check digit
+            return $.fn.bootstrapValidator.helpers.luhn(value);
         }
     };
 }(window.jQuery));