programer.tips, another cool thing from @nghuuphuoc!

zipCode validator

Validate a zip code

Validators

Improve it on Github

Options

Option HTML attribute Type Description
country data-bv-zipcode-country String An ISO-3166 country code.
It's dynamic option
message data-bv-zipcode-message String The error message
When setting options via HTML attributes, remember to enable the validator by setting data-bv-zipcode="true".

Currently, the validator supports the following countries:

Country Code
United States US
Brazil BR
Canada CA
Czech Republic CZ
Denmark DK
Italy IT
Morocco MA
Netherlands NL
Romania RO
Russia RU
Slovakia SK
Singapore SG
Sweden SE
United Kingdom GB

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).ready(function() {
    $('#postcodeForm').bootstrapValidator({
        fields: {
            postcode: {
                validators: {
                    regexp: {
                        regexp: /^\d{5}$/,
                        message: 'The US zipcode must contain 5 digits'
                    }
                }
            }
        }
    });
});

Example

UK Postcode Result
EC1A 1BB
W1A 1HQ
M1 1AA
B33 8TH
CR2 6XH
DN55 1PT
AI-2640
ASCN 1ZZ
GIR 0AA
<form id="zipCodeForm" class="form-horizontal">
    <div class="form-group">
        <label class="col-sm-3 control-label">UK postcode</label>
        <div class="col-sm-5">
            <input type="text" class="form-control" name="postcode" />
        </div>
    </div>
</form>
$(document).ready(function() {
    $('#zipCodeForm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            postcode: {
                validators: {
                    zipCode: {
                        country: 'GB',
                        message: 'The value is not valid UK postcode'
                    }
                }
            }
        }
    });
});

Related validators

The following validators might be useful to you: