浏览代码

Merge pull request #650 from fhferreira/patch-4

Add Brazil VAT number validator
Phuoc Nguyen 11 年之前
父节点
当前提交
3c72a22eb9
共有 1 个文件被更改,包括 64 次插入1 次删除
  1. 64 1
      src/js/validator/vat.js

+ 64 - 1
src/js/validator/vat.js

@@ -7,6 +7,7 @@
             AT: 'Austrian',
             BE: 'Belgian',
             BG: 'Bulgarian',
+            BR: 'Brazil',
             CH: 'Swiss',
             CY: 'Cypriot',
             CZ: 'Czech',
@@ -50,7 +51,7 @@
 
         // Supported country codes
         COUNTRY_CODES: [
-            'AT', 'BE', 'BG', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU',
+            'AT', 'BE', 'BG', 'BR', 'CH', 'CY', 'CZ', 'DE', 'DK', 'EE', 'EL', 'ES', 'FI', 'FR', 'GB', 'GR', 'HR', 'HU',
             'IE', 'IS', 'IT', 'LT', 'LU', 'LV', 'MT', 'NL', 'NO', 'PL', 'PT', 'RO', 'RU', 'RS', 'SE', 'SK', 'SI', 'ZA'
         ],
 
@@ -256,6 +257,68 @@
 
             return false;
         },
+        
+        /**
+         * Validate Brazilian VAT number (CNPJ)
+         *
+         * @param {String} value VAT number
+         * @returns {Boolean}
+         */
+        _br: function(value) {
+            if (value === '') {
+                return true;
+            }
+            cnpj = value.replace(/[^\d]+/g, '');
+
+            if (cnpj == '') return false;
+
+            if (cnpj.length != 14)
+                return false;
+
+            // Remove invalids CNPJs 
+            if (cnpj == "00000000000000" ||
+                cnpj == "11111111111111" ||
+                cnpj == "22222222222222" ||
+                cnpj == "33333333333333" ||
+                cnpj == "44444444444444" ||
+                cnpj == "55555555555555" ||
+                cnpj == "66666666666666" ||
+                cnpj == "77777777777777" ||
+                cnpj == "88888888888888" ||
+                cnpj == "99999999999999")
+                return false;
+
+            // Validate verification digits
+            length = cnpj.length - 2
+            numbers = cnpj.substring(0, length);
+            digits = cnpj.substring(length);
+            sum = 0;
+            pos = length - 7;
+            for (i = length; i >= 1; i--) {
+                sum += numbers.charAt(length - i) * pos--;
+                if (pos < 2)
+                    pos = 9;
+            }
+            result = sum % 11 < 2 ? 0 : 11 - sum % 11;
+            if (result != digits.charAt(0))
+                return false;
+
+            length = length + 1;
+            numbers = cnpj.substring(0, length);
+            sum = 0;
+            pos = length - 7;
+            for (i = length; i >= 1; i--) {
+                sum += numbers.charAt(length - i) * pos--;
+                if (pos < 2)
+                    pos = 9;
+            }
+            result = sum % 11 < 2 ? 0 : 11 - sum % 11;
+            if (result != digits.charAt(1))
+                return false;
+
+            return true;
+        },
+        
 
         /**
          * Validate Swiss VAT number