zipCode validator

Validate a zip code

Options

Using with form field

* presents a required option. The HTML attributes are used to set the validator options via the Declarative plugin

Name HTML attribute Type Description
country* data-fv-zip-code___country String or Function An ISO-3166 country code
message data-fv-zip-code___message String The error message

If you want to support custom formats of a zipCode number, you should use the Transformer plugin.

The validator supports the following countries (click the sample number to validate it):

Country Country code Sample Is it valid?
United States US 12345
123
Austria AT 6020
1010
4853
0020
12345
102
12AB
AT 6020 XY
Bulgaria BG
Brazil BR
Canada CA
Czech Republic CZ 12345
123 45
12 345
123456
1 2345
1234 5
12 3 45
Denmark DK DK-1234
DK 123
France FR 12340
01230
75116
123 45
12 345
123456
1 2345
1234 5
12 3 45
1234A
Germany DE 52238
01001
09107
01000
99999
102
ABCDE
DE 52240 XY
India IN 226024
201014
226 021
226-024
11023
A226023
Italy IT I-12345
IT-12345
1234
Ireland IE A65 F4E2
D6W FNT4
T37 F8HK
a65 f4e2
D6W FNTO
T37F8HK
Morocco MA
Netherlands NL 1234 ab
0123
Poland PL 02-920
00-002
77-400
03456
000-02
AB-002
12 345
Portugal PT 2435-459
1000-000
1234-456
0123-456
1234456
1234-ABC
1234 456
Romania RO
Russia RU
Singapore SG
Slovakia SK 12345
123 45
12 345
123456
1 2345
1234 5
12 3 45
Spain ES 01234
28080
29004
41023
0123
99999
102
ABCD
00000
Sweden SE S-12345
S-567 8
Switzerland CH 8280
8090
8238
9490
0123
99999
102
ABCD
CH-5224 XY
United Kingdom GB EC1A 1BB
W1A 1HQ
M1 1AA
B33 8TH
CR2 6XH
DN55 1PT
AI-2640
ASCN 1ZZ
GIR 0AA

US zipcode

This validator supports 4 digits US zipcode. If you want a valid US zipcode to be 5 digits exactly, use the regexp validator



document.addEventListener('DOMContentLoaded', function(e) {
    FormValidation.formValidation(
        document.getElementById('demoForm'),
        {
            fields: {
                postcode: {
                    validators: {
                        regexp: {
                            regexp: /^d{5}$/,
                            message: 'The US zip code must contain 5 digits'
                        }
                    }
                }
            },
        }
    );
});

Using with JavaScript module


// You might need to change the importing path
import zipCode from 'formvalidation/validators/zipCode';

const result = zipCode().validate({
    value: ...,
    options: {
        // Can be a string or a function returns a string
        country: ...,
        message: ...,
    },
});
/*
result is an object of
{
    valid: true or false,
    message: The error message
}
*/

Basic example

You can click on sample from the table above to see if it's a valid or invalid zipcode.

Using country option with different value

In the example above, the value of country select box must be the country code as following.



In the case that the value attribute of option can't exactly be the country code, instead, be a country name for example:



Then we need to set the country option as a callback function returning a country code based on the selected name:



document.addEventListener('DOMContentLoaded', function(e) {
    const form = document.getElementById('demoForm');
    const fv = FormValidation.formValidation(
        form,
        {
            fields: {
                postalCode: {
                    validators: {
                        zipCode: {
                            country: function() {
                                // Map the country names to the code
                                const map = {
                                    'United States': 'US',
                                    'Austria': 'AT',
                                    'Bulgaria': 'BG',
                                    'Brazil': 'BR',
                                    'Canada': 'CA',
                                    'Czech Republic': 'CZ',
                                    'Denmark': 'DK',
                                    'French': 'FR',
                                    'Germany': 'DE',
                                    'India': 'IN',
                                    'Italy': 'IT',
                                    'Morocco': 'MA',
                                    'Netherlands': 'NL',
                                    'Poland': 'PL',
                                    'Portugal': 'PT',
                                    'Romania': 'RO',
                                    'Russia': 'RU',
                                    'Singapore': 'SG',
                                    'Slovakia': 'SK',
                                    'Spain': 'ES',
                                    'Sweden': 'SE',
                                    'Switzerland': 'CH',
                                    'United Kingdom': 'GB'
                                };

                                // Get the selected country
                                const country = form.querySelector('[name="country"]').value;

                                // Return the country code based on selected name
                                return (country == '') ? '' : (map[country] || '');
                            },
                            message: 'The value is not a valid postal code'
                        }
                    }
                },
            },
            plugins: {
                ...,
            },
        }
    );

    form.querySelector('[name="country"]').addEventListener('change', function() {
        // Revalidate the postal code field whenever user changes the country
        fv.revalidateField('postalCode');
    });
});

This case happens when you can't control the value of country select box which might be generated by a back-end side or third party.

Module Example

The following snippet shows how to use the zipCode validator with JavaScript module:


// You might need to change the importing path
import zipCode from 'formvalidation/validators/zipCode';

const res1 = zipCode().validate({
    value: '12345',
    options: {
        country: 'US',
        message: 'The value is not a valid zipcode',
    },
});
// res1.valid === true

const res2 = zipCode().validate({
    value: '12345',
    options: {
        country: 'AT',
        message: 'The value is not a valid zipcode',
    },
});
// res2.valid === false

Related validators

The following validators are similar to this one and might be useful to you.

Name Description
phone Validate a phone number