ソースを参照

Merge pull request #1072 from thomaslhotta/master

Added zip code validation for Austria and Switzerland
Phuoc Nguyen 11 年 前
コミット
cf6ef84f8d
3 ファイル変更89 行追加9 行削除
  1. 2 0
      src/js/language/de_DE.js
  2. 19 7
      src/js/validator/zipCode.js
  3. 68 2
      test/spec/validator/zipCode.js

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

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

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

@@ -4,8 +4,10 @@
         countryNotSupported: 'The country code %s is not supported',
         country: 'Please enter a valid postal code in %s',
         countries: {
+            AT: 'Austria',
             BR: 'Brazil',
             CA: 'Canada',
+            CH: 'Switzerland',
             CZ: 'Czech Republic',
             DE: 'Germany',
             DK: 'Denmark',
@@ -31,7 +33,7 @@
             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
@@ -75,6 +77,11 @@
             var isValid = false;
             country = country.toUpperCase();
             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':
                     isValid = /^(\d{2})([\.]?)(\d{3})([\-]?)(\d{3})$/.test(value);
                     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);
                     break;
 
+                case 'CH':
+                    isValid = /^([1-9]{1})(\d{3})$/.test(value);
+                    break;
+
                 case 'CZ':
                     // Test: http://regexr.com/39hhr
                     isValid = /^(\d{3})([ ]?)(\d{2})$/.test(value);
                     break;
 
+                // http://stackoverflow.com/questions/7926687/regular-expression-german-zip-codes
                 case 'DE':
-                    isValid = /^([01245678][0-9]{4})$/.test(value);
+                    isValid = /^(?!01000|99999)(0[1-9]\d{3}|[1-9]\d{4})$/.test(value);
                     break;
 
                 case 'DK':
                     isValid = /^(DK(-|\s)?)?\d{4}$/i.test(value);
                     break;
-                    
+
                 // http://en.wikipedia.org/wiki/Postal_codes_in_France
                 case 'FR':
                     isValid = /^[0-9]{5}$/i.test(value);
@@ -104,7 +116,7 @@
                 case 'GB':
                     isValid = this._gb(value);
                     break;
-                
+
                 // http://www.eircode.ie/docs/default-source/Common/prepare-your-business-for-eircode---published-v2.pdf?sfvrsn=2
                 // Test: http://refiddle.com/1kpl
                 case 'IE':
@@ -125,12 +137,12 @@
                 case 'NL':
                     isValid = /^[1-9][0-9]{3} ?(?!sa|sd|ss)[a-z]{2}$/i.test(value);
                     break;
-                
+
                 // Test: http://refiddle.com/1l2t
                 case 'PT':
                     isValid = /^[1-9]\d{3}-\d{3}$/.test(value);
                     break;
-                    
+
                 case 'RO':
                     isValid = /^(0[1-8]{1}|[1-9]{1}[0-5]{1})?[0-9]{4}$/i.test(value);
                     break;
@@ -145,7 +157,7 @@
 
                 case 'SG':
                     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':
                     // Test: http://regexr.com/39hhr

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

@@ -273,7 +273,7 @@ describe('zipCode', function() {
             expect(this.bv.isValid()).toEqual(false);
         }
     });
-    
+
     it('Eircode (Ireland postal code)', function() {
         this.bv.updateOption('zc', 'zipCode', 'country', 'IE');
 
@@ -295,7 +295,7 @@ describe('zipCode', function() {
             expect(this.bv.isValid()).toEqual(false);
         }
     });
-    
+
     it('Portugal postal code', function() {
         this.bv.updateOption('zc', 'zipCode', 'country', 'PT');
 
@@ -317,4 +317,70 @@ describe('zipCode', function() {
             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);
+        }
+    });
 });