Browse Source

#187, #232: Validate Brazilian ID

phuoc 11 years ago
parent
commit
a1ee2cd1b6
4 changed files with 155 additions and 2 deletions
  1. 1 0
      CHANGELOG.md
  2. 76 0
      dist/js/bootstrapValidator.js
  3. 2 2
      dist/js/bootstrapValidator.min.js
  4. 76 0
      src/js/validator/id.js

+ 1 - 0
CHANGELOG.md

@@ -3,6 +3,7 @@
 ## v0.4.5 (not released yet)
 
 * [#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)
 
 ## v0.4.4 (2014-05-05)

+ 76 - 0
dist/js/bootstrapValidator.js

@@ -1798,6 +1798,82 @@
     };
 }(window.jQuery));
 ;(function($) {
+    $.fn.bootstrapValidator.validators.id = {
+        html5Attributes: {
+            message: 'message',
+            country: 'country'
+        },
+
+        /**
+         * Validate identification number in different countries
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - message: The invalid message
+         * - country: The ISO 3166-1 country code
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var country = options.country || value.substr(0, 2),
+                method  = ['_', country.toLowerCase()].join('');
+            if (this[method] && 'function' == typeof this[method]) {
+                return this[method](value);
+            }
+
+            return true;
+        },
+
+        /**
+         * Validate Brazilian ID (CFP)
+         * Examples:
+         * - Valid: 39053344705, 390.533.447-05, 111.444.777-35
+         * - Invalid: 231.002.999-00
+         *
+         * @see http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas
+         * @param {String} value The ID
+         * @returns {Boolean}
+         */
+        _br: function(value) {
+            if (/^1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}|0{11}$/.test(value)) {
+                return false;
+            }
+            if (!/^\d{11}$/.test(value) && !/^\d{3}\.\d{3}\.\d{3}-\d{2}$/.test(value)) {
+                return false;
+            }
+            value = value.replace(/\./g, '').replace(/-/g, '');
+
+            var d1 = 0;
+            for (var i = 0; i < 9; i++) {
+                d1 += (10 - i) * parseInt(value.charAt(i));
+            }
+            d1 = 11 - d1 % 11;
+            if (d1 == 10 || d1 == 11) {
+                d1 = 0;
+            }
+            if (d1 != value.charAt(9)) {
+                return false;
+            }
+
+            var d2 = 0;
+            for (i = 0; i < 10; i++) {
+                d2 += (11 - i) * parseInt(value.charAt(i));
+            }
+            d2 = 11 - d2 % 11;
+            if (d2 == 10 || d2 == 11) {
+                d2 = 0;
+            }
+
+            return (d2 == value.charAt(10));
+        }
+    };
+}(window.jQuery));
+;(function($) {
     $.fn.bootstrapValidator.validators.identical = {
         html5Attributes: {
             message: 'message',

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


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

@@ -0,0 +1,76 @@
+(function($) {
+    $.fn.bootstrapValidator.validators.id = {
+        html5Attributes: {
+            message: 'message',
+            country: 'country'
+        },
+
+        /**
+         * Validate identification number in different countries
+         *
+         * @param {BootstrapValidator} validator The validator plugin instance
+         * @param {jQuery} $field Field element
+         * @param {Object} options Consist of key:
+         * - message: The invalid message
+         * - country: The ISO 3166-1 country code
+         * @returns {Boolean}
+         */
+        validate: function(validator, $field, options) {
+            var value = $field.val();
+            if (value == '') {
+                return true;
+            }
+
+            var country = options.country || value.substr(0, 2),
+                method  = ['_', country.toLowerCase()].join('');
+            if (this[method] && 'function' == typeof this[method]) {
+                return this[method](value);
+            }
+
+            return true;
+        },
+
+        /**
+         * Validate Brazilian ID (CFP)
+         * Examples:
+         * - Valid: 39053344705, 390.533.447-05, 111.444.777-35
+         * - Invalid: 231.002.999-00
+         *
+         * @see http://en.wikipedia.org/wiki/Cadastro_de_Pessoas_F%C3%ADsicas
+         * @param {String} value The ID
+         * @returns {Boolean}
+         */
+        _br: function(value) {
+            if (/^1{11}|2{11}|3{11}|4{11}|5{11}|6{11}|7{11}|8{11}|9{11}|0{11}$/.test(value)) {
+                return false;
+            }
+            if (!/^\d{11}$/.test(value) && !/^\d{3}\.\d{3}\.\d{3}-\d{2}$/.test(value)) {
+                return false;
+            }
+            value = value.replace(/\./g, '').replace(/-/g, '');
+
+            var d1 = 0;
+            for (var i = 0; i < 9; i++) {
+                d1 += (10 - i) * parseInt(value.charAt(i));
+            }
+            d1 = 11 - d1 % 11;
+            if (d1 == 10 || d1 == 11) {
+                d1 = 0;
+            }
+            if (d1 != value.charAt(9)) {
+                return false;
+            }
+
+            var d2 = 0;
+            for (i = 0; i < 10; i++) {
+                d2 += (11 - i) * parseInt(value.charAt(i));
+            }
+            d2 = 11 - d2 % 11;
+            if (d2 == 10 || d2 == 11) {
+                d2 = 0;
+            }
+
+            return (d2 == value.charAt(10));
+        }
+    };
+}(window.jQuery));