Hi All,
If you want validate your Zipcode,phone or fax number as per US rule.
Because phone number has 10 digit and zipcode has total 9 digit.but 5 digit also valid zip number in US.
So we need to validate both(All)the scenario.
If you will used masked(Masking) input for this type of input that's very good for you.
================================================
jQuery.validator.addMethod("ZipCodeRule", function(zip) {
var i;
for (i = 1; i <= 9; i++) { zip = zip.replace(/([_])/, ""); } if (zip.length == 0 || (zip.length == 1 && zip.charAt(0) == "-") || zip.match(/^\d{5}([- ]?\d{4})?$/) || zip.match(/^\\d{5}$/) || zip.length == 6) { return true; } return false; }, "Enter valid Zip Code");
================================================
/*If Phone is not mandatory*/ jQuery.validator.addMethod("PhoneNumberRule", function(Phone, element) { Phone = Phone.replace(/\s+/g, ""); for (i = 1; i <= 10; i++) { Phone = Phone.replace(/([_])/, ""); } if (Phone.length == 0 || (Phone.length == 3 && Phone.charAt(1) == ")")) return true; else return this.optional(element) || Phone.length > 9 &&
Phone.match(/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/);
}, "Enter Valid Phone Number");
================================================
/*If FaxNumber is not mandatory*/
jQuery.validator.addMethod("FaxNumberRule", function(Fax, element) {
Fax = Fax.replace(/\s+/g, "");
for (i = 1; i <= 10; i++) { Fax = Fax.replace(/([_])/, ""); } if (Fax.length == 0 || (Fax.length == 3 && Fax.charAt(1) == ")")) return true; else return this.optional(element) || Fax.length > 9 &&
Fax.match(/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/);
}, "Enter Valid Fax Number");
================================================
/*If Pager is not mandatory*/
jQuery.validator.addMethod("PagerNumberRule", function(Pager, element) {
Pager = Pager.replace(/\s+/g, "");
for (i = 1; i <= 10; i++) { Pager = Pager.replace(/([_])/, ""); } if (Pager.length == 0 || (Pager.length == 3 && Pager.charAt(1) == ")")) return true; else return this.optional(element) || Pager.length > 9 &&
Pager.match(/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/);
}, "Enter Valid Pager Number");
================================================
Thanks
If you want validate your Zipcode,phone or fax number as per US rule.
Because phone number has 10 digit and zipcode has total 9 digit.but 5 digit also valid zip number in US.
So we need to validate both(All)the scenario.
If you will used masked(Masking) input for this type of input that's very good for you.
================================================
jQuery.validator.addMethod("ZipCodeRule", function(zip) {
var i;
for (i = 1; i <= 9; i++) { zip = zip.replace(/([_])/, ""); } if (zip.length == 0 || (zip.length == 1 && zip.charAt(0) == "-") || zip.match(/^\d{5}([- ]?\d{4})?$/) || zip.match(/^\\d{5}$/) || zip.length == 6) { return true; } return false; }, "Enter valid Zip Code");
================================================
/*If Phone is not mandatory*/ jQuery.validator.addMethod("PhoneNumberRule", function(Phone, element) { Phone = Phone.replace(/\s+/g, ""); for (i = 1; i <= 10; i++) { Phone = Phone.replace(/([_])/, ""); } if (Phone.length == 0 || (Phone.length == 3 && Phone.charAt(1) == ")")) return true; else return this.optional(element) || Phone.length > 9 &&
Phone.match(/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/);
}, "Enter Valid Phone Number");
================================================
/*If FaxNumber is not mandatory*/
jQuery.validator.addMethod("FaxNumberRule", function(Fax, element) {
Fax = Fax.replace(/\s+/g, "");
for (i = 1; i <= 10; i++) { Fax = Fax.replace(/([_])/, ""); } if (Fax.length == 0 || (Fax.length == 3 && Fax.charAt(1) == ")")) return true; else return this.optional(element) || Fax.length > 9 &&
Fax.match(/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/);
}, "Enter Valid Fax Number");
================================================
/*If Pager is not mandatory*/
jQuery.validator.addMethod("PagerNumberRule", function(Pager, element) {
Pager = Pager.replace(/\s+/g, "");
for (i = 1; i <= 10; i++) { Pager = Pager.replace(/([_])/, ""); } if (Pager.length == 0 || (Pager.length == 3 && Pager.charAt(1) == ")")) return true; else return this.optional(element) || Pager.length > 9 &&
Pager.match(/^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/);
}, "Enter Valid Pager Number");
================================================
Thanks
No comments:
Post a Comment
Share your thoughts....