Demo of validating different ZIP formats
Country specific ZIP code or Postal index number ( PIN ) validation
6 digits, Can't start with 0
Valid:462010, 123456
Invalid:012345, IN1234
/^[1-9][0-9]{5}$/
4 digits, First two digits range from 10 to 43
Valid:4110, 1099
Invalid:4433, AF34
/^([1-3][0-9]|4[0-3])[0-9]{2}$/
5 digits,Range 97300 - 97390
Valid:97350, 97390
Invalid:97299, GF380
/^973([0-8][0-9]|90)$/
After entering zip1 press
Tab or click outside text but to trigger
blur event of the text box.
Click the text box and then press
Tab to edit the entry.
HTML
<div class="row">
<div class="col-md-4"><span class='form-group'>
<label class='col-md-3 control-label' for='zip1'>zip1</label>
<span class='col-md-8'><input type=text name=zip1 id=zip1 class='form-control'> </span></span></div>
<div class="col-md-4">
<div id=msg_zip1></div>
</div></div>
jquery
<script>
$(document).ready(function() {
///////////////////
$('#AF').blur(function(){
var AF=$('#AF').val();
var regex = /^([1-3][0-9]|4[0-3])[0-9]{2}$/;
if(regex.test(AF)){
$('#AF').closest('.form-group').removeClass('has-error');
$('#AF').closest('.form-group').addClass('form-group has-success has-feedback');
$('#msg_AF').removeClass('bg-danger').addClass('bg-success');
$('#AF').addClass('glyphicon glyphicon-ok');
$('#msg_AF').html("<span class='glyphicon glyphicon-ok'></span> Valid Afghanistan Pin Code");
}else{
$('#msg_AF').html('Failed');
$('#AF').closest('.form-group').removeClass('has-success');
$('#AF').closest('.form-group').addClass('has-error');
$('#msg_AF').addClass('bg-danger');
$('#msg_AF').html("<span class='glyphicon glyphicon-remove' aria-hidden='true'></span>Not a valid Afghanistan Pin code");
}
$('#msg_AF').show();
});
///////////////////
})
</script>