Validate a zip code by a country code.
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 |
Currently, the validator supports the following countries:
Country | Code |
---|---|
United States | US |
Brazil | BR |
Canada | CA |
Denmark | DK |
Italy | IT |
Morocco | MA |
Netherlands | NL |
Sweden | SE |
Singapore | SG |
United Kingdom | GB |
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'
}
}
}
}
});
});
UK Postcode | Result |
---|---|
EC1A 1BB | |
W1A 1HQ | |
M1 1AA | |
B33 8TH | |
CR2 6XH | |
DN55 1PT | |
AI-2640 | |
ASCN 1ZZ | |
GIR 0AA |
<form id="postcodeForm" class="form-horizontal">
<div class="form-group">
<label class="col-lg-3 control-label">UK postcode</label>
<div class="col-lg-5">
<input type="text" class="form-control" name="postcode" />
</div>
</div>
</form>
$(document).ready(function() {
$('#postcodeForm').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'
}
}
}
}
});
});
The following validators might be useful to you: