Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mobile number validation for US and India #9

Open
klpatil opened this issue Jan 20, 2014 · 0 comments
Open

Mobile number validation for US and India #9

klpatil opened this issue Jan 20, 2014 · 0 comments

Comments

@klpatil
Copy link

klpatil commented Jan 20, 2014

Nice library! Thanks for this!

We are trying to incorporate your library in our solution. And we've two questions to ask you:

  1. We've country names and based on that we would like to validate phone number for example :
    isValidNumber("PHONENUMBER", "COUNTRY NAME"); e.g.

isValidNumber("9998063528", "India");

Now, to do that, we've created one function which returns country code from Country name e.g.

var aCountryCode = countryNameToCode("India");

/Convert the country name to a code
country name (String) to 2 digit country code
/
function countryNameToCode(countryName) {

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

// TODO : Do we need to store country name in array while storing? I think we don't need it
// that's why removing this code -- countryName.toUpperCase()
var code = arrCountryCode[countryName];
if (code === undefined) {
    return "";
} else {
    return code;
}

}

Is it fine? Or there is better way to do it?

  1. Also, when we open http://phoneformat.com/ and enter any U.S. number and select "India" then only it says it's valid number. This is how it works? Logically it is right as total number's count is 10. But just wanted to have your views.

Thanks,
Kiran

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant