Saturday, 18 April 2015

JavaScript Geography

Most of this information is available on the net. I have compiled into a single document for easy reading.

JavaScript code is after the descriptions.

Enum Countries

var enum = geography.enumCountries;

  • geography.US gives 'US'
  • geography.CA gives 'CA'
  • geography.AU gives 'AU'
  • geography.USA gives 'USA'
  • geography.CAN gives 'CAN'

 

 Enum Phone Regular Expression

var enum = geography.enumPhoneRegularExpressions;

  • geography.USCanada will give regular expression for US and Canada phone numbers
  • geography.Australia will give regular expression for Australia phone number

 

Enum Zip Code Regular Expression

var enum = geography.enumZipCodesRegularExpression;

  • geography.US will give regular expression for US
  • geography.Canada will give regular expression for Canada
  • geography.Australia will give regular expression for Australia

 

Area List

Array list for US, USA, CA, CAN, AU. US and USA are the same, CA and CAN are the same
var areaList = geography.area;
Object has following properties

  • Country: code
  • CountryName: country name
  • States: state codes array

 

Get Country Code By Code

var code = geography.getCountryCodeByCode(code);

 

Get Country Name By Code

var name = geography.getCountryNameByCode(code);

 

Check State By Country

var isOk = geography.checkStateInCountry(countryCode, stateCode);
returns true/false

 

Check Zip Code By Country Code

var isOk = geography.checkZipCodeByCountryCode(countryCode, zipCode);
returns true/false

 

Check Phone By Country Code

var isOk = geography.checkPhoneByCountryCode(countryCode, phone);
returns true/false

 

Make Phone Standard

var phone = geography.makePhoneStandard(countryCode, phone);


Geography.js

var geography = {
    enumGetStatesDropDown: {
        TriggerControlId: '', //control that fired the states pull
        StateDropDownUrl: '',
        StateSelected: '', //for pre-selection
        CountryCode: '', //country for which we are pulling the states
        StyleClass: '',
        ContentContainerId: '', //container in which we will display the states
        StateControlId: 'SelectedState', //text box or drop down id/name,
        RelAttribute: '',
        AltAttribute: ''
    },
    enumCountries: {
        US: 'US', CA: 'CA', AU: 'AU', USA: 'USA', CAN: 'CAN'
    },
    enumPhoneRegularExpressions: {
        USCanada: /\d{3}\d{3}\d{4}|\d{3}-\d{3}-\d{4}/,
        Australia: /(^1300\d{6}$)|(^1800|1900|1902\d{6}$)|(^0[2|3|7|8]{1}[0-9]{8}$)|(^13\d{4}$)|(^04\d{2,3}\d{6}$)/
        /*
        Australian phone numbers: Matches all known formats incl normal 10-digit landline
        numbers (valid area code mandatory) 13, 1300, 1800, 1900, 1902 plus mobile 10 and
        11-digit formats. Use a Replace function first to remove non-numerics which are
        probably separators (E.g. newNum = number.replace(/[\D]/g, "").
        Matches 0732105432 | 1300333444 | 131313
        Non-Matches 32105432 | 13000456
        */
    },
    enumZipCodesRegularExpression: {
        US: /^([0-9]{5})$/, //5 digit zip code **** /^\d{5}([\-]\d{4})?$/; //9 digit zip code
        Canada: /^[ABCEGHJKLMNPRSTVXYabceghjklmnprstvxy]{1}\d{1}[A-Za-z]{1} *\d{1}[A-Za-z]{1}\d{1}$/,
        Australia: /^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$/
        /*
        Australia has 4-digit numeric postal codes with the following state based specific ranges.
        ACT: 0200-0299 and 2600-2639.
        NSW: 1000-1999, 2000-2599 and 2640-2914.
        NT: 0900-0999 and 0800-0899.
        QLD: 9000-9999 and 4000-4999.
        SA: 5000-5999.
        TAS: 7800-7999 and 7000-7499.
        VIC: 8000-8999 and 3000-3999.
        WA: 6800-6999 and 6000-6799.
        Matches 0200 | 7312 | 2415
        Non-Matches 0300 | 7612 | 2915
        */
    },
    area: [
        {
            Country: "US",
            CountryName: "United States",
            States: ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "PR", "VI", "GU", "AS"]
        },
        {
            Country: "USA",
            CountryName: "United States",
            States: ["AL", "AK", "AZ", "AR", "CA", "CO", "CT", "DE", "DC", "FL", "GA", "HI", "ID", "IL", "IN", "IA", "KS", "KY", "LA", "ME", "MD", "MA", "MI", "MN", "MS", "MO", "MT", "NE", "NV", "NH", "NJ", "NM", "NY", "NC", "ND", "OH", "OK", "OR", "PA", "RI", "SC", "SD", "TN", "TX", "UT", "VT", "VA", "WA", "WV", "WI", "WY", "PR", "VI", "GU", "AS"]
        },
        {
            Country: "CA",
            CountryName: "Canada",
            States: ["AB", "BC", "MB", "NB", "NL", "NT", "NS", "NU", "ON", "PE", "QC", "SK", "YT"]
        },
        {
            Country: "CAN",
            CountryName: "Canada",
            States: ["AB", "BC", "MB", "NB", "NL", "NT", "NS", "NU", "ON", "PE", "QC", "SK", "YT"]
        },
        {
            Country: "AU",
            CountryName: "Australia",
            States: ["ACT", "NSW", "NT", "QLD", "SA", "VIC", "WA"]
        }
    ],
    getCountryCodeByCode: function (code) {
        //make a local reference to area to use here
        var area = geography.area;
        var areaCode = '';
        for (var i = 0; i < area.length; i++) {
            if (code == area[i].Country)
                areaCode = area[i].Country;
        }
        return areaCode;
    },
    getCountryNameByCode: function (code) {
        //make a local reference to area to use here
        var area = geography.area;
        var areaName = '';
        for (var i = 0; i < area.length; i++) {
            if (code.toUpperCase() == area[i].Country.toUpperCase())
                areaName = area[i].CountryName;
        }
        return areaName;
    },
    checkStateInCountry: function (countryCode, stateCode) {
        //make a local reference to area to use here
        var area = geography.area;
        var blnReturn = false;
        var states;
        for (var i = 0; i < area.length; i++) {
            if (countryCode.toUpperCase() == area[i].Country.toUpperCase()) {
                states = area[i].States;
                if (states.toString().indexOf(stateCode) != -1)
                    blnReturn = true;
            }
        }
        return blnReturn;
    },
    checkZipCodeByCountryCode: function (countryCode, zipCode) {
        var enumCountries = geography.enumCountries;
        var blnReturn = true;
        switch (countryCode.toUpperCase()) {
            case enumCountries.US:
            case enumCountries.USA:
                var expression = geography.enumZipCodesRegularExpression.US;
                blnReturn = expression.test(zipCode);
                break;
            case enumCountries.CA:
            case enumCountries.CAN:
                var expression = geography.enumZipCodesRegularExpression.Canada;
                blnReturn = expression.test(zipCode);
                break;
            case enumCountries.AU:
                var expression = geography.enumZipCodesRegularExpression.Australia;
                blnReturn = expression.test(zipCode);
                break;
            default:
                blnReturn = true;
        };
        return blnReturn;
    },
    checkPhoneByCountryCode: function (countryCode, phone) {
        var enumCountries = geography.enumCountries;
        var blnReturn = true;
        switch (countryCode.toUpperCase()) {
            case enumCountries.US:
            case enumCountries.USA:
            case enumCountries.CA:
            case enumCountries.CAN:
                phone = phone.replace(/-/g, "");
                var expression = geography.enumPhoneRegularExpressions.USCanada;
                blnReturn = expression.test(phone);
                break;
            case enumCountries.AU:
                var expression = geography.enumPhoneRegularExpressions.Australia;
                blnReturn = expression.test(phone);
                break;
        };
        return blnReturn;
    },
    makePhoneStandard: function (countryCode, phone) {
        var enumCountries = geography.enumCountries;
        var strReturn = '';
        if (phone != '') {
            switch (countryCode.toUpperCase()) {
                case enumCountries.US:
                case enumCountries.USA:
                case enumCountries.CA:
                case enumCountries.CAN:
                    var phone2 = '' + phone;
                    phone2 = phone2.replace(/[-]/g, "");
                    phone2 = phone2.replace(/[\s]/g, "");
                    phone2 = phone2.replace(/[(]/g, "");
                    phone2 = phone2.replace(/[)]/g, "");
                    if (phone2.length == 10 && string.isNumber(phone2)) {
                        var cFirst = phone2.substring(0, 3);
                        var cMiddle = phone2.substring(3, 6);
                        var cLast = phone2.substring(6, 10);
                        strReturn = cFirst + "-" + cMiddle + "-" + cLast;
                    }
                    break;
                default:
                    strReturn = phone;
                    break;
            };
        }
        return strReturn;
    },
    getStatesDropDown: function () {
        var enumState = geography.enumGetStatesDropDown;
        var countryCode = enumState.CountryCode;
        var contentContainerId = enumState.ContentContainerId;
        var stateDropDownUrl = enumState.StateDropDownUrl;
        var triggerControlId = enumState.TriggerControlId;

        var $click = $("#" + triggerControlId);
        if (!$click.jquery)
            $click = '';

        if (contentContainerId == '' || countryCode == '' || stateDropDownUrl == '') {
            jMessageError("Unable to complete request as important information is missing!", $click, false, true);
        }
        else {
            jMessage('Processing request...', $click, true, false);
            $.post(stateDropDownUrl, { countryCode: enumState.CountryCode, selectedState: enumState.StateSelected, styleClass: enumState.StyleClass, relAttribute: enumState.RelAttribute, altAttribute: enumState.AltAttribute, random: Math.random() }, function (data) {
                //show data
                $("#" + contentContainerId).html(data).fadeIn();
                jMessageHide();
            });
        }
    }
};