File tree 2 files changed +13
-4
lines changed
2 files changed +13
-4
lines changed Original file line number Diff line number Diff line change @@ -83,13 +83,18 @@ func (f Format) CheckRegion(region string) bool {
83
83
//
84
84
// An empty postal code is considered valid.
85
85
func (f Format ) CheckPostalCode (postalCode string ) bool {
86
- if postalCode == "" || f . PostalCodePattern == "" {
86
+ if postalCode == "" {
87
87
return true
88
88
}
89
- rx := regexp .MustCompile (f .PostalCodePattern )
89
+ rx := regexp .MustCompile (f .PostalCodeValidationPattern () )
90
90
return rx .MatchString (postalCode )
91
91
}
92
92
93
+ // PostalCodeValidationPattern returns the full regex pattern for validating the postal code.
94
+ func (f * Format ) PostalCodeValidationPattern () string {
95
+ return "^" + f .PostalCodePattern + "$"
96
+ }
97
+
93
98
// SelectLayout selects the correct layout for the given locale.
94
99
func (f Format ) SelectLayout (locale Locale ) string {
95
100
if f .LocalLayout != "" && f .useLocalData (locale ) {
Original file line number Diff line number Diff line change @@ -108,9 +108,13 @@ func TestFormat_CheckPostalCode(t *testing.T) {
108
108
// Valid postal code.
109
109
{"FR" , "75002" , true },
110
110
// Invalid postal code.
111
- {"FR" , "INVALID" , false },
111
+ {"FR" , "A75002" , false },
112
+ // Invalid postal code.
113
+ {"FR" , "75002B" , false },
114
+ // Country with no predefined pattern.
115
+ {"AG" , "AG123" , false },
112
116
// Country with no predefined pattern.
113
- {"AG" , "AG123 " , true },
117
+ {"AG" , "" , true },
114
118
}
115
119
116
120
for _ , tt := range tests {
You can’t perform that action at this time.
0 commit comments