JavaScript - Get country name or code - sample

kevinhng86

Member
Registered
Joined
Jan 12, 2017
Messages
58
Points
0
Hello all,

Have you ever get in situation where you need to convert an user's country iso code to country name. This code is a perfect solution for you. This Javascript function is one of a few Javascript library that I modified and extend it ability to have the capability to produce either country name or country code base on what information being pass into the function. I would like to share this. I love sharing small library function that I wrote.

This function intake a string value. The string value can be a country name or country code. It does not matter on whether if which letter is capitalise or not. What does matter is if the name or the code contain within it's database. For instance if you fetch this function with the word "af", it will spit out "Afghanistan". If you fetch it "afghanistan or Afghanistan or afgHanistan", it will spit out "af".

This function return an array value. Array index 0 is the convert value. If the country name or code is not found in the database it will return an error string. Array index 1 will return the original value that was fetch into the function. This is really great where you want to record what went into the script.


Code:
function getCountryCodeOrName (inputValue) {
var nameCountries = {
'Afghanistan' : 'AF',
'Aland Islands' : 'AX',
'Albania' : 'AL',
'Algeria' : 'DZ',
'American Samoa' : 'AS',
'Andorra' : 'AD',
'Angola' : 'AO',
'Anguilla' : 'AI',
'Antarctica' : 'AQ',
'Antigua And Barbuda' : 'AG',
'Argentina' : 'AR',
'Armenia' : 'AM',
'Aruba' : 'AW',
'Australia' : 'AU',
'Austria' : 'AT',
'Azerbaijan' : 'AZ',
'Bahamas' : 'BS',
'Bahrain' : 'BH',
'Bangladesh' : 'BD',
'Barbados' : 'BB',
'Belarus' : 'BY',
'Belgium' : 'BE',
'Belize' : 'BZ',
'Benin' : 'BJ',
'Bermuda' : 'BM',
'Bhutan' : 'BT',
'Bolivia' : 'BO',
'Bosnia And Herzegovina' : 'BA',
'Botswana' : 'BW',
'Bouvet Island' : 'BV',
'Brazil' : 'BR',
'British Indian Ocean Territory' : 'IO',
'Brunei Darussalam' : 'BN',
'Bulgaria' : 'BG',
'Burkina Faso' : 'BF',
'Burundi' : 'BI',
'Cambodia' : 'KH',
'Cameroon' : 'CM',
'Canada' : 'CA',
'Cape Verde' : 'CV',
'Cayman Islands' : 'KY',
'Central African Republic' : 'CF',
'Chad' : 'TD',
'Chile' : 'CL',
'China' : 'CN',
'Christmas Island' : 'CX',
'Cocos (Keeling) Islands' : 'CC',
'Colombia' : 'CO',
'Comoros' : 'KM',
'Congo' : 'CG',
'Congo, Democratic Republic' : 'CD',
'Cook Islands' : 'CK',
'Costa Rica' : 'CR',
'Cote D\'Ivoire' : 'CI',
'Croatia' : 'HR',
'Cuba' : 'CU',
'Cyprus' : 'CY',
'Czech Republic' : 'CZ',
'Denmark' : 'DK',
'Djibouti' : 'DJ',
'Dominica' : 'DM',
'Dominican Republic' : 'DO',
'Ecuador' : 'EC',
'Egypt' : 'EG',
'El Salvador' : 'SV',
'Equatorial Guinea' : 'GQ',
'Eritrea' : 'ER',
'Estonia' : 'EE',
'Ethiopia' : 'ET',
'Falkland Islands (Malvinas)' : 'FK',
'Faroe Islands' : 'FO',
'Fiji' : 'FJ',
'Finland' : 'FI',
'France' : 'FR',
'French Guiana' : 'GF',
'French Polynesia' : 'PF',
'French Southern Territories' : 'TF',
'Gabon' : 'GA',
'Gambia' : 'GM',
'Georgia' : 'GE',
'Germany' : 'DE',
'Ghana' : 'GH',
'Gibraltar' : 'GI',
'Greece' : 'GR',
'Greenland' : 'GL',
'Grenada' : 'GD',
'Guadeloupe' : 'GP',
'Guam' : 'GU',
'Guatemala' : 'GT',
'Guernsey' : 'GG',
'Guinea' : 'GN',
'Guinea-Bissau' : 'GW',
'Guyana' : 'GY',
'Haiti' : 'HT',
'Heard Island & Mcdonald Islands' : 'HM',
'Holy See (Vatican City State)' : 'VA',
'Honduras' : 'HN',
'Hong Kong' : 'HK',
'Hungary' : 'HU',
'Iceland' : 'IS',
'India' : 'IN',
'Indonesia' : 'ID',
'Iran, Islamic Republic Of' : 'IR',
'Iraq' : 'IQ',
'Ireland' : 'IE',
'Isle Of Man' : 'IM',
'Israel' : 'IL',
'Italy' : 'IT',
'Jamaica' : 'JM',
'Japan' : 'JP',
'Jersey' : 'JE',
'Jordan' : 'JO',
'Kazakhstan' : 'KZ',
'Kenya' : 'KE',
'Kiribati' : 'KI',
'Korea' : 'KR',
'Kuwait' : 'KW',
'Kyrgyzstan' : 'KG',
'Lao People\'s Democratic Republic' : 'LA',
'Latvia' : 'LV',
'Lebanon' : 'LB',
'Lesotho' : 'LS',
'Liberia' : 'LR',
'Libyan Arab Jamahiriya' : 'LY',
'Liechtenstein' : 'LI',
'Lithuania' : 'LT',
'Luxembourg' : 'LU',
'Macao' : 'MO',
'Macedonia' : 'MK',
'Madagascar' : 'MG',
'Malawi' : 'MW',
'Malaysia' : 'MY',
'Maldives' : 'MV',
'Mali' : 'ML',
'Malta' : 'MT',
'Marshall Islands' : 'MH',
'Martinique' : 'MQ',
'Mauritania' : 'MR',
'Mauritius' : 'MU',
'Mayotte' : 'YT',
'Mexico' : 'MX',
'Micronesia, Federated States Of' : 'FM',
'Moldova' : 'MD',
'Monaco' : 'MC',
'Mongolia' : 'MN',
'Montenegro' : 'ME',
'Montserrat' : 'MS',
'Morocco' : 'MA',
'Mozambique' : 'MZ',
'Myanmar' : 'MM',
'Namibia' : 'NA',
'Nauru' : 'NR',
'Nepal' : 'NP',
'Netherlands' : 'NL',
'Netherlands Antilles' : 'AN',
'New Caledonia' : 'NC',
'New Zealand' : 'NZ',
'Nicaragua' : 'NI',
'Niger' : 'NE',
'Nigeria' : 'NG',
'Niue' : 'NU',
'Norfolk Island' : 'NF',
'Northern Mariana Islands' : 'MP',
'Norway' : 'NO',
'Oman' : 'OM',
'Pakistan' : 'PK',
'Palau' : 'PW',
'Palestinian Territory, Occupied' : 'PS',
'Panama' : 'PA',
'Papua New Guinea' : 'PG',
'Paraguay' : 'PY',
'Peru' : 'PE',
'Philippines' : 'PH',
'Pitcairn' : 'PN',
'Poland' : 'PL',
'Portugal' : 'PT',
'Puerto Rico' : 'PR',
'Qatar' : 'QA',
'Reunion' : 'RE',
'Romania' : 'RO',
'Russian Federation' : 'RU',
'Rwanda' : 'RW',
'Saint Barthelemy' : 'BL',
'Saint Helena' : 'SH',
'Saint Kitts And Nevis' : 'KN',
'Saint Lucia' : 'LC',
'Saint Martin' : 'MF',
'Saint Pierre And Miquelon' : 'PM',
'Saint Vincent And Grenadines' : 'VC',
'Samoa' : 'WS',
'San Marino' : 'SM',
'Sao Tome And Principe' : 'ST',
'Saudi Arabia' : 'SA',
'Senegal' : 'SN',
'Serbia' : 'RS',
'Seychelles' : 'SC',
'Sierra Leone' : 'SL',
'Singapore' : 'SG',
'Slovakia' : 'SK',
'Slovenia' : 'SI',
'Solomon Islands' : 'SB',
'Somalia' : 'SO',
'South Africa' : 'ZA',
'South Georgia And Sandwich Isl.' : 'GS',
'Spain' : 'ES',
'Sri Lanka' : 'LK',
'Sudan' : 'SD',
'Suriname' : 'SR',
'Svalbard And Jan Mayen' : 'SJ',
'Swaziland' : 'SZ',
'Sweden' : 'SE',
'Switzerland' : 'CH',
'Syrian Arab Republic' : 'SY',
'Taiwan' : 'TW',
'Tajikistan' : 'TJ',
'Tanzania' : 'TZ',
'Thailand' : 'TH',
'Timor-Leste' : 'TL',
'Togo' : 'TG',
'Tokelau' : 'TK',
'Tonga' : 'TO',
'Trinidad And Tobago' : 'TT',
'Tunisia' : 'TN',
'Turkey' : 'TR',
'Turkmenistan' : 'TM',
'Turks And Caicos Islands' : 'TC',
'Tuvalu' : 'TV',
'Uganda' : 'UG',
'Ukraine' : 'UA',
'United Arab Emirates' : 'AE',
'United Kingdom' : 'GB',
'United States' : 'US',
'United States Outlying Islands' : 'UM',
'Uruguay' : 'UY',
'Uzbekistan' : 'UZ',
'Vanuatu' : 'VU',
'Venezuela' : 'VE',
'Viet Nam' : 'VN',
'Virgin Islands, British' : 'VG',
'Virgin Islands, U.S.' : 'VI',
'Wallis And Futuna' : 'WF',
'Western Sahara' : 'EH',
'Yemen' : 'YE',
'Zambia' : 'ZM',
'Zimbabwe' : 'ZW'
}

  var errorCode = "";

  if (inputValue === undefined || Boolean(inputValue.match(/^[^,'& ()-]{1}([a-zA-Z\s',&()-]){0,80}$/)) === false){errorCode = "5555"}
  var inlength = inputValue.length ;
  if (errorCode === "5555"){
  		return ["ErrorCode 5555: Unsupported Data, return input value in array 1",inputValue];
  }

  if ( inlength === 2){
    var outValue = inputValue.toUpperCase();
    var countryName = "";
    for ( var name in nameCountries){
         if (nameCountries[name] === outValue){
         	countryName = name ;
         }
    }
    switch (true){
    case Boolean(countryName === ""):
    return ["Country code does not match database record: return input value in array 1",inputValue];
    break;

    default:
    return [countryName,inputValue];
    break;
    }

  }

  if ( inlength > 2) {
  var outValue = inputValue.toLowerCase();

  switch (true){
  case Boolean(outValue.match(/^[Gg][Uu][Ii][Nn][Ee][Aa][-][Bb][Ii][Ss][Ss][Aa][Uu]$/)):
  outValue = "Guinea-Bissau";
  break;

  case Boolean(outValue.match(/^[Hh][Oo][Ll][Yy][ ][Ss][Ee][Ee][ ][(][Vv][Aa][Tt][Ii][Cc][Aa][Nn][ ][Cc][Ii][Tt][Yy][ ][Ss][Tt][Aa][Tt][Ee][)]$/)):
  outValue = "Holy See (Vatican City State)";
  break;

  case Boolean(outValue.match(/[Ff][Aa][Ll][Kk][Ll][Aa][Nn][Dd][ ][Ii][Ss][Ll][Aa][Nn][Dd][Ss][ ][(][Mm][Aa][Ll][Vv][Ii][Nn][Aa][Ss][)]$/)):
  outValue = "Falkland Islands (Malvinas)";
  break;

  case Boolean(outValue.match(/^[Ii][Rr][Aa][Nn]$/)):
  outValue = "Iran, Islamic Republic Of";
  break;

  default:
  var arrCName = outValue.split(" ");
  var cNameJoin = "";
  for ( var i in arrCName) {
  	  cNameJoin += arrCName[i].charAt(0).toUpperCase() + arrCName[i].slice(1) + " ";
  }
  outValue = cNameJoin.trim();
  break;
}


  return [
  (nameCountries.hasOwnProperty(outValue)?nameCountries[outValue]:"Input country doesn't match database: returned input in array 1"),
  inputValue
  ];
}}
 

MotionGrafika

Member
Registered
Joined
Apr 17, 2016
Messages
52
Points
6
Nice one!

How to see its result when running on a browser?

I tried to paste it into a HTML file and wrap around it with this

Code:
<script type="text/javascript">
//your codes
</script>
But it is showing an empty page.

How can I test your code and see it works.
 
Older Threads
Newer Threads
Replies
6
Views
20,603
Replies
8
Views
7,138
Replies
2
Views
3,378
Latest Threads
Recommended Threads

Latest Hosting OffersNew Reviews

Sponsors

Tag Cloud

You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an alternative browser.

Top