Browse Source

Added zip code validators for Austria and Switzerland. Improved zip code validator for Germany. Added unit tests Austria, Germany and Switzerland. Updated translations.

Thomas Lhotta 11 years ago
parent
commit
713b5cf449

+ 1 - 1
dist/css/bootstrapValidator.min.css

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
  *
- * @version     v0.5.3-dev, built on 2014-11-03 3:09:43 PM
+ * @version     v0.5.3-dev, built on 2014-11-03 12:37:06 PM
  * @author      https://twitter.com/nghuuphuoc
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
  * @license     MIT

+ 20 - 8
dist/js/bootstrapValidator.js

@@ -2,7 +2,7 @@
  * BootstrapValidator (http://bootstrapvalidator.com)
  * BootstrapValidator (http://bootstrapvalidator.com)
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  * The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3
  *
  *
- * @version     v0.5.3-dev, built on 2014-11-03 3:09:43 PM
+ * @version     v0.5.3-dev, built on 2014-11-03 12:37:06 PM
  * @author      https://twitter.com/nghuuphuoc
  * @author      https://twitter.com/nghuuphuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @copyright   (c) 2013 - 2014 Nguyen Huu Phuoc
  * @license     MIT
  * @license     MIT
@@ -8006,8 +8006,10 @@ if (typeof jQuery === 'undefined') {
         countryNotSupported: 'The country code %s is not supported',
         countryNotSupported: 'The country code %s is not supported',
         country: 'Please enter a valid postal code in %s',
         country: 'Please enter a valid postal code in %s',
         countries: {
         countries: {
+            AT: 'Austria',
             BR: 'Brazil',
             BR: 'Brazil',
             CA: 'Canada',
             CA: 'Canada',
+            CH: 'Switzerland',
             CZ: 'Czech Republic',
             CZ: 'Czech Republic',
             DE: 'Germany',
             DE: 'Germany',
             DK: 'Denmark',
             DK: 'Denmark',
@@ -8033,7 +8035,7 @@ if (typeof jQuery === 'undefined') {
             country: 'country'
             country: 'country'
         },
         },
 
 
-        COUNTRY_CODES: ['BR', 'CA', 'CZ', 'DE', 'DK', 'FR', 'GB', 'IE', 'IT', 'MA', 'NL', 'PT', 'RO', 'RU', 'SE', 'SG', 'SK', 'US'],
+        COUNTRY_CODES: [ 'AT', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'FR', 'GB', 'IE', 'IT', 'MA', 'NL', 'PT', 'RO', 'RU', 'SE', 'SG', 'SK', 'US'],
 
 
         /**
         /**
          * Return true if and only if the input value is a valid country zip code
          * Return true if and only if the input value is a valid country zip code
@@ -8077,6 +8079,11 @@ if (typeof jQuery === 'undefined') {
             var isValid = false;
             var isValid = false;
             country = country.toUpperCase();
             country = country.toUpperCase();
             switch (country) {
             switch (country) {
+                // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Austria
+                case 'AT':
+                    isValid = /^([1-9]{1})(\d{3})$/.test(value);
+                    break;
+
                 case 'BR':
                 case 'BR':
                     isValid = /^(\d{2})([\.]?)(\d{3})([\-]?)(\d{3})$/.test(value);
                     isValid = /^(\d{2})([\.]?)(\d{3})([\-]?)(\d{3})$/.test(value);
                     break;
                     break;
@@ -8085,19 +8092,24 @@ if (typeof jQuery === 'undefined') {
                     isValid = /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}[0-9]{1}$/i.test(value);
                     isValid = /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}[0-9]{1}$/i.test(value);
                     break;
                     break;
 
 
+                case 'CH':
+                    isValid = /^([1-9]{1})(\d{3})$/.test(value);
+                    break;
+
                 case 'CZ':
                 case 'CZ':
                     // Test: http://regexr.com/39hhr
                     // Test: http://regexr.com/39hhr
                     isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
                     isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
                     break;
                     break;
 
 
+                // http://stackoverflow.com/questions/7926687/regular-expression-german-zip-codes
                 case 'DE':
                 case 'DE':
-                    isValid = /^([01245678][0-9]{4})$/.test(value);
+                    isValid = /^(?!01000|99999)(0[1-9]\d{3}|[1-9]\d{4})$/.test(value);
                     break;
                     break;
 
 
                 case 'DK':
                 case 'DK':
                     isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
                     isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
                     break;
                     break;
-                    
+
                 // http://en.wikipedia.org/wiki/Postal_codes_in_France
                 // http://en.wikipedia.org/wiki/Postal_codes_in_France
                 case 'FR':
                 case 'FR':
                     isValid = /^[0-9]{5}$/i.test(value);
                     isValid = /^[0-9]{5}$/i.test(value);
@@ -8106,7 +8118,7 @@ if (typeof jQuery === 'undefined') {
                 case 'GB':
                 case 'GB':
                     isValid = this._gb(value);
                     isValid = this._gb(value);
                     break;
                     break;
-                
+
                 // http://www.eircode.ie/docs/default-source/Common/prepare-your-business-for-eircode---published-v2.pdf?sfvrsn=2
                 // http://www.eircode.ie/docs/default-source/Common/prepare-your-business-for-eircode---published-v2.pdf?sfvrsn=2
                 // Test: http://refiddle.com/1kpl
                 // Test: http://refiddle.com/1kpl
                 case 'IE':
                 case 'IE':
@@ -8127,12 +8139,12 @@ if (typeof jQuery === 'undefined') {
                 case 'NL':
                 case 'NL':
                     isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
                     isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
                     break;
                     break;
-                
+
                 // Test: http://refiddle.com/1l2t
                 // Test: http://refiddle.com/1l2t
                 case 'PT':
                 case 'PT':
                     isValid = /^[1-9]\d{3}-\d{3}$/.test(value);
                     isValid = /^[1-9]\d{3}-\d{3}$/.test(value);
                     break;
                     break;
-                    
+
                 case 'RO':
                 case 'RO':
                     isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
                     isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
                     break;
                     break;
@@ -8147,7 +8159,7 @@ if (typeof jQuery === 'undefined') {
 
 
                 case 'SG':
                 case 'SG':
                     isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
                     isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
-                    break;                
+                    break;
 
 
                 case 'SK':
                 case 'SK':
                     // Test: http://regexr.com/39hhr
                     // Test: http://regexr.com/39hhr

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


+ 2 - 0
dist/js/language/de_DE.js

@@ -344,8 +344,10 @@
             countryNotSupported: 'Der Ländercode %s wird nicht unterstützt',
             countryNotSupported: 'Der Ländercode %s wird nicht unterstützt',
             country: 'Bitte gültigen Postleitzahl für %s eingeben',
             country: 'Bitte gültigen Postleitzahl für %s eingeben',
             countries: {
             countries: {
+                AT: 'Österreich',
                 BR: 'Brasilien',
                 BR: 'Brasilien',
                 CA: 'Kanada',
                 CA: 'Kanada',
+                CH: 'Schweiz',
                 CZ: 'Tschechische',
                 CZ: 'Tschechische',
                 DE: 'Deutschland',
                 DE: 'Deutschland',
                 DK: 'Dänemark',
                 DK: 'Dänemark',

+ 2 - 0
src/js/language/de_DE.js

@@ -344,8 +344,10 @@
             countryNotSupported: 'Der Ländercode %s wird nicht unterstützt',
             countryNotSupported: 'Der Ländercode %s wird nicht unterstützt',
             country: 'Bitte gültigen Postleitzahl für %s eingeben',
             country: 'Bitte gültigen Postleitzahl für %s eingeben',
             countries: {
             countries: {
+                AT: 'Österreich',
                 BR: 'Brasilien',
                 BR: 'Brasilien',
                 CA: 'Kanada',
                 CA: 'Kanada',
+                CH: 'Schweiz',
                 CZ: 'Tschechische',
                 CZ: 'Tschechische',
                 DE: 'Deutschland',
                 DE: 'Deutschland',
                 DK: 'Dänemark',
                 DK: 'Dänemark',

+ 19 - 7
src/js/validator/zipCode.js

@@ -4,8 +4,10 @@
         countryNotSupported: 'The country code %s is not supported',
         countryNotSupported: 'The country code %s is not supported',
         country: 'Please enter a valid postal code in %s',
         country: 'Please enter a valid postal code in %s',
         countries: {
         countries: {
+            AT: 'Austria',
             BR: 'Brazil',
             BR: 'Brazil',
             CA: 'Canada',
             CA: 'Canada',
+            CH: 'Switzerland',
             CZ: 'Czech Republic',
             CZ: 'Czech Republic',
             DE: 'Germany',
             DE: 'Germany',
             DK: 'Denmark',
             DK: 'Denmark',
@@ -31,7 +33,7 @@
             country: 'country'
             country: 'country'
         },
         },
 
 
-        COUNTRY_CODES: ['BR', 'CA', 'CZ', 'DE', 'DK', 'FR', 'GB', 'IE', 'IT', 'MA', 'NL', 'PT', 'RO', 'RU', 'SE', 'SG', 'SK', 'US'],
+        COUNTRY_CODES: [ 'AT', 'BR', 'CA', 'CH', 'CZ', 'DE', 'DK', 'FR', 'GB', 'IE', 'IT', 'MA', 'NL', 'PT', 'RO', 'RU', 'SE', 'SG', 'SK', 'US'],
 
 
         /**
         /**
          * Return true if and only if the input value is a valid country zip code
          * Return true if and only if the input value is a valid country zip code
@@ -75,6 +77,11 @@
             var isValid = false;
             var isValid = false;
             country = country.toUpperCase();
             country = country.toUpperCase();
             switch (country) {
             switch (country) {
+                // http://en.wikipedia.org/wiki/List_of_postal_codes_in_Austria
+                case 'AT':
+                    isValid = /^([1-9]{1})(\d{3})$/.test(value);
+                    break;
+
                 case 'BR':
                 case 'BR':
                     isValid = /^(\d{2})([\.]?)(\d{3})([\-]?)(\d{3})$/.test(value);
                     isValid = /^(\d{2})([\.]?)(\d{3})([\-]?)(\d{3})$/.test(value);
                     break;
                     break;
@@ -83,19 +90,24 @@
                     isValid = /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}[0-9]{1}$/i.test(value);
                     isValid = /^(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|X|Y){1}[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}\s?[0-9]{1}(?:A|B|C|E|G|H|J|K|L|M|N|P|R|S|T|V|W|X|Y|Z){1}[0-9]{1}$/i.test(value);
                     break;
                     break;
 
 
+                case 'CH':
+                    isValid = /^([1-9]{1})(\d{3})$/.test(value);
+                    break;
+
                 case 'CZ':
                 case 'CZ':
                     // Test: http://regexr.com/39hhr
                     // Test: http://regexr.com/39hhr
                     isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
                     isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
                     break;
                     break;
 
 
+                // http://stackoverflow.com/questions/7926687/regular-expression-german-zip-codes
                 case 'DE':
                 case 'DE':
-                    isValid = /^([01245678][0-9]{4})$/.test(value);
+                    isValid = /^(?!01000|99999)(0[1-9]\d{3}|[1-9]\d{4})$/.test(value);
                     break;
                     break;
 
 
                 case 'DK':
                 case 'DK':
                     isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
                     isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
                     break;
                     break;
-                    
+
                 // http://en.wikipedia.org/wiki/Postal_codes_in_France
                 // http://en.wikipedia.org/wiki/Postal_codes_in_France
                 case 'FR':
                 case 'FR':
                     isValid = /^[0-9]{5}$/i.test(value);
                     isValid = /^[0-9]{5}$/i.test(value);
@@ -104,7 +116,7 @@
                 case 'GB':
                 case 'GB':
                     isValid = this._gb(value);
                     isValid = this._gb(value);
                     break;
                     break;
-                
+
                 // http://www.eircode.ie/docs/default-source/Common/prepare-your-business-for-eircode---published-v2.pdf?sfvrsn=2
                 // http://www.eircode.ie/docs/default-source/Common/prepare-your-business-for-eircode---published-v2.pdf?sfvrsn=2
                 // Test: http://refiddle.com/1kpl
                 // Test: http://refiddle.com/1kpl
                 case 'IE':
                 case 'IE':
@@ -125,12 +137,12 @@
                 case 'NL':
                 case 'NL':
                     isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
                     isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
                     break;
                     break;
-                
+
                 // Test: http://refiddle.com/1l2t
                 // Test: http://refiddle.com/1l2t
                 case 'PT':
                 case 'PT':
                     isValid = /^[1-9]\d{3}-\d{3}$/.test(value);
                     isValid = /^[1-9]\d{3}-\d{3}$/.test(value);
                     break;
                     break;
-                    
+
                 case 'RO':
                 case 'RO':
                     isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
                     isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
                     break;
                     break;
@@ -145,7 +157,7 @@
 
 
                 case 'SG':
                 case 'SG':
                     isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
                     isValid = /^([0][1-9]|[1-6][0-9]|[7]([0-3]|[5-9])|[8][0-2])(\d{4})$/i.test(value);
-                    break;                
+                    break;
 
 
                 case 'SK':
                 case 'SK':
                     // Test: http://regexr.com/39hhr
                     // Test: http://regexr.com/39hhr

+ 68 - 2
test/spec.js

@@ -8236,7 +8236,7 @@ describe('zipCode', function() {
             expect(this.bv.isValid()).toEqual(false);
             expect(this.bv.isValid()).toEqual(false);
         }
         }
     });
     });
-    
+
     it('Eircode (Ireland postal code)', function() {
     it('Eircode (Ireland postal code)', function() {
         this.bv.updateOption('zc', 'zipCode', 'country', 'IE');
         this.bv.updateOption('zc', 'zipCode', 'country', 'IE');
 
 
@@ -8258,7 +8258,7 @@ describe('zipCode', function() {
             expect(this.bv.isValid()).toEqual(false);
             expect(this.bv.isValid()).toEqual(false);
         }
         }
     });
     });
-    
+
     it('Portugal postal code', function() {
     it('Portugal postal code', function() {
         this.bv.updateOption('zc', 'zipCode', 'country', 'PT');
         this.bv.updateOption('zc', 'zipCode', 'country', 'PT');
 
 
@@ -8280,4 +8280,70 @@ describe('zipCode', function() {
             expect(this.bv.isValid()).toEqual(false);
             expect(this.bv.isValid()).toEqual(false);
         }
         }
     });
     });
+
+    it('Austria postal code', function() {
+        this.bv.updateOption('zc', 'zipCode', 'country', 'AT');
+
+        // Valid samples
+        var validSamples = ['6020', '1010', '4853'];
+        for (var i in validSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(validSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toBeTruthy();
+        }
+
+        // Invalid samples
+        var invalidSamples = ['0020', '12345', '102', '12AB', 'AT 6020 XY'];
+        for (i in invalidSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(invalidSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toEqual(false);
+        }
+    });
+
+    it('Germany postal code', function() {
+        this.bv.updateOption('zc', 'zipCode', 'country', 'DE');
+
+        // Valid samples
+        var validSamples = ['52238', '01001', '09107'];
+        for (var i in validSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(validSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toBeTruthy();
+        }
+
+        // Invalid samples
+        var invalidSamples = ['01000', '99999', '102', 'ABCDE', 'DE 52240 XY'];
+        for (i in invalidSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(invalidSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toEqual(false);
+        }
+    });
+
+    it('Switzerland postal code', function() {
+        this.bv.updateOption('zc', 'zipCode', 'country', 'CH');
+
+        // Valid samples
+        var validSamples = [ '8280', '8090', '8238', '9490'];
+        for (var i in validSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(validSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toBeTruthy();
+        }
+
+        // Invalid samples
+        var invalidSamples = ['0123', '99999', '102', 'ABCD', 'CH-5224 XY'];
+        for (i in invalidSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(invalidSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toEqual(false);
+        }
+    });
 });
 });

+ 68 - 2
test/spec/validator/zipCode.js

@@ -273,7 +273,7 @@ describe('zipCode', function() {
             expect(this.bv.isValid()).toEqual(false);
             expect(this.bv.isValid()).toEqual(false);
         }
         }
     });
     });
-    
+
     it('Eircode (Ireland postal code)', function() {
     it('Eircode (Ireland postal code)', function() {
         this.bv.updateOption('zc', 'zipCode', 'country', 'IE');
         this.bv.updateOption('zc', 'zipCode', 'country', 'IE');
 
 
@@ -295,7 +295,7 @@ describe('zipCode', function() {
             expect(this.bv.isValid()).toEqual(false);
             expect(this.bv.isValid()).toEqual(false);
         }
         }
     });
     });
-    
+
     it('Portugal postal code', function() {
     it('Portugal postal code', function() {
         this.bv.updateOption('zc', 'zipCode', 'country', 'PT');
         this.bv.updateOption('zc', 'zipCode', 'country', 'PT');
 
 
@@ -317,4 +317,70 @@ describe('zipCode', function() {
             expect(this.bv.isValid()).toEqual(false);
             expect(this.bv.isValid()).toEqual(false);
         }
         }
     });
     });
+
+    it('Austria postal code', function() {
+        this.bv.updateOption('zc', 'zipCode', 'country', 'AT');
+
+        // Valid samples
+        var validSamples = ['6020', '1010', '4853'];
+        for (var i in validSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(validSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toBeTruthy();
+        }
+
+        // Invalid samples
+        var invalidSamples = ['0020', '12345', '102', '12AB', 'AT 6020 XY'];
+        for (i in invalidSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(invalidSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toEqual(false);
+        }
+    });
+
+    it('Germany postal code', function() {
+        this.bv.updateOption('zc', 'zipCode', 'country', 'DE');
+
+        // Valid samples
+        var validSamples = ['52238', '01001', '09107'];
+        for (var i in validSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(validSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toBeTruthy();
+        }
+
+        // Invalid samples
+        var invalidSamples = ['01000', '99999', '102', 'ABCDE', 'DE 52240 XY'];
+        for (i in invalidSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(invalidSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toEqual(false);
+        }
+    });
+
+    it('Switzerland postal code', function() {
+        this.bv.updateOption('zc', 'zipCode', 'country', 'CH');
+
+        // Valid samples
+        var validSamples = [ '8280', '8090', '8238', '9490'];
+        for (var i in validSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(validSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toBeTruthy();
+        }
+
+        // Invalid samples
+        var invalidSamples = ['0123', '99999', '102', 'ABCD', 'CH-5224 XY'];
+        for (i in invalidSamples) {
+            this.bv.resetForm();
+            this.$zipCode.val(invalidSamples[i]);
+            this.bv.validate();
+            expect(this.bv.isValid()).toEqual(false);
+        }
+    });
 });
 });