Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

ZIP Code (Postal Code) validation - UK, US, Canada RegExp

 Example:

function postalCodeCheck(postalCode, country) {

    if(!postalCode) {

        return false;

    }

    postalCode = postalCode.toString().trim();

    var us = new RegExp("^\\d{5}(-{0,1}\\d{4})?$");

    var ca = new RegExp(/^[ABCEGHJKLMNPRSTVXY]\d[ABCEGHJKLMNPRSTVWXYZ]( )?\d[ABCEGHJKLMNPRSTVWXYZ]\d$/i);

    var uk = /^[A-Z]{1,2}[0-9RCHNQ][0-9A-Z]?\s?[0-9][ABD-HJLNP-UW-Z]{2}$|^[A-Z]{2}-?[0-9]{4}$/;

    var other = new RegExp("^\\d+$");

    if(country == "US") {

        if(us.test(postalCode.toString())) {

            return true;

        }

    }

    else if (country == "CA") {

        if(ca.test(postalCode.toString())) {

            return true;

        }

    }

    else if (country == "UK") {

        if(uk.test(postalCode.toString())) {

            return true;

        }

    }

    else {

        if(other.test(postalCode.toString())) {

           return true

        }

    }

    return false;

}

//postalCodeCheck("SW1W 0NY", "UK"); //True

Share the post

ZIP Code (Postal Code) validation - UK, US, Canada RegExp

×

Subscribe to Angular 2 Javascript Asp.net C# Sql Server Knockoutjs Angularjs Kendo Ui Html 5 Web Api Wpf Wcf Etc.

Get updates delivered right to your inbox!

Thank you for your subscription

×