I would like to share my Experience of verifying US postal Codes in Jquery that I have used during a project in Oodles Technologies.
I have solved this problem by using jQuery validation plugin
$("#myform").validate({
rules: {
field: {
required: true,
phoneUS: true
}
}
});
Files required for this
<script src="http://code.jquery.com/jquery-1.11.1.min.js"></script>
<script src="http://jqueryvalidation.org/files/dist/jquery.validate.min.js"></script
It sometimes doesn't work.
To get it working we have to include Jquery addtional-methods.min.js
If still it doesn't work then you have to use following script before validate method which contains regex to validate.
jQuery.validator.addMethod("phoneUS", function (phone_number, element) {
phone_number = phone_number.replace(/\s+/g, "");
return this.optional(element) || phone_number.length > 9 &&
phone_number.match(/\(?[\d\s]{3}\)[\d\s]{3}-[\d\s]{4}$/);
}, "Invalid phone number");
It finally solve the problem. If you have required some other format then modify above regex according to need.
if (v.match(/[13]\d+/) { alert('Not in Coverage')