validatePhoneNumber static method
for phone number validation
Implementation
static bool validatePhoneNumber(String phoneNumber,String countryCode) {
Map<String, dynamic> countryPhoneLengths = {
'AF': 9, // Afghanistan
'AL': 9, // Albania
'DZ': 9, // Algeria
'US': 10, // United States
'AD': 6, // Andorra
'AO': 9, // Angola
'AR': [10, 11], // Argentina (varies by region)
'AM': 8, // Armenia
'AU': 9, // Australia (excluding country code)
'AT': [9, 10], // Austria (varies)
'AZ': 9, // Azerbaijan
'BH': 8, // Bahrain
'BD': 10, // Bangladesh
'BY': 9, // Belarus
'BE': 9, // Belgium
'BZ': 7, // Belize
'BJ': 8, // Benin
'BT': 8, // Bhutan
'BO': 8, // Bolivia
'BA': 8, // Bosnia and Herzegovina
'BW': 7, // Botswana
'BR': [10, 11], // Brazil (10 for landline, 11 for mobile)
'BN': 7, // Brunei
'BG': 9, // Bulgaria
'BF': 8, // Burkina Faso
'BI': 8, // Burundi
'KH': 9, // Cambodia
'CM': 9, // Cameroon
'CA': 10, // Canada
'CV': 7, // Cape Verde
'CF': 8, // Central African Republic
'TD': 8, // Chad
'CL': 9, // Chile
'CN': 11, // China
'CO': 10, // Colombia
'KM': 7, // Comoros
'CG': 9, // Congo
'CD': 9, // Congo, Democratic Republic of the
'CK': 5, // Cook Islands
'CR': 8, // Costa Rica
'HR': 9, // Croatia
'CU': 8, // Cuba
'CY': 8, // Cyprus
'CZ': 9, // Czech Republic
'DK': 8, // Denmark
'DJ': 6, // Djibouti
'DM': 10, // Dominica
'DO': 10, // Dominican Republic
'EC': 9, // Ecuador
'EG': [9, 10], // Egypt (9 for landline, 10 for mobile)
'SV': 8, // El Salvador
'GQ': 9, // Equatorial Guinea
'ER': 7, // Eritrea
'EE': 7, // Estonia
'ET': 9, // Ethiopia
'FJ': 7, // Fiji
'FI': [9, 10], // Finland (9 or 10 digits)
'FR': 9, // France
'GA': 7, // Gabon
'GM': 7, // Gambia
'GE': 9, // Georgia
'DE': [10,11], // Germany
'GH': 9, // Ghana
'GR': 10, // Greece
'GL': 6, // Greenland
'GT': 8, // Guatemala
'GN': 9, // Guinea
'GW': 7, // Guinea-Bissau
'GY': 7, // Guyana
'HT': 8, // Haiti
'HN': 8, // Honduras
'HK': 8, // Hong Kong
'HU': 9, // Hungary
'IS': 7, // Iceland
'IN': 10, // India
'ID': [10, 12], // Indonesia (varies)
'IR': 10, // Iran
'IQ': [9, 10], // Iraq (varies)
'IE': 9, // Ireland
'IL': 9, // Israel
'IT': 10, // Italy
'CI': 8, // Ivory Coast
'JM': 7, // Jamaica
'JP': 10, // Japan
'JO': 9, // Jordan
'KZ': 10, // Kazakhstan
'KE': 10, // Kenya
'KI': 8, // Kiribati
'KW': 8, // Kuwait
'KG': 9, // Kyrgyzstan
'LA': 9, // Laos
'LV': 8, // Latvia
'LB': 8, // Lebanon
'LS': 8, // Lesotho
'LR': 7, // Liberia
'LY': 10, // Libya
'LI': 7, // Liechtenstein
'LT': 8, // Lithuania
'LU': [8, 9], // Luxembourg (varies)
'MO': 8, // Macau
'MK': 8, // Macedonia
'MG': 9, // Madagascar
'MW': 9, // Malawi
'MY': [9,10], // Malaysia
'MV': 7, // Maldives
'ML': 8, // Mali
'MT': 8, // Malta
'MH': 7, // Marshall Islands
'MR': 8, // Mauritania
'MU': 8, // Mauritius
'MX': 10, // Mexico
'FM': 7, // Micronesia
'MD': 8, // Moldova
'MC': 8, // Monaco
'MN': 8, // Mongolia
'ME': 8, // Montenegro
'MA': 9, // Morocco
'MZ': 9, // Mozambique
'MM': 9, // Myanmar
'NA': 9, // Namibia
'NR': 7, // Nauru
'NP': 10, // Nepal
'NL': 9, // Netherlands
'NZ': [8, 9], // New Zealand (some numbers are 9 digits)
'NI': 8, // Nicaragua
'NE': 8, // Niger
'NG': 10, // Nigeria
'NO': 8, // Norway
'OM': 8, // Oman
'PK': 10, // Pakistan
'PW': 7, // Palau
'PA': 8, // Panama
'PG': [7, 8], // Papua New Guinea (landline is 7, mobile is 8)
'PY': 9, // Paraguay
'PE': 9, // Peru
'PH': 10, // Philippines
'PL': 9, // Poland
'PT': 9, // Portugal
'QA': 8, // Qatar
'RO': 10, // Romania
'RU': 10, // Russia
'RW': 9, // Rwanda
'WS': 7, // Samoa
'SM': 9, // San Marino
'ST': 7, // Sao Tome and Principe
'SA': 9, // Saudi Arabia
'SN': 9, // Senegal
'RS': 9, // Serbia
'SC': 7, // Seychelles
'SL': 8, // Sierra Leone
'SG': 8, // Singapore
'SK': 9, // Slovakia
'SI': 9, // Slovenia
'SB': 7, // Solomon Islands
'SO': [7, 8], // Somalia (landline: 7, mobile: 8)
'ZA': 9, // South Africa
'KR': 10, // South Korea
'SS': 9, // South Sudan
'ES': 9, // Spain
'LK': 9, // Sri Lanka
'SD': 9, // Sudan
'SR': 7, // Suriname
'SZ': 8, // Eswatini
'SE': [7, 9], // Sweden (landline: 7, mobile: 9)
'CH': 9, // Switzerland
'SY': 9, // Syria
'TW': 9, // Taiwan
'TJ': 9, // Tajikistan
'TZ': 9, // Tanzania
'TH': 9, // Thailand
'TG': 8, // Togo
'TO': 7, // Tonga
'TN': 8, // Tunisia
'TR': 10, // Turkey
'TM': 8, // Turkmenistan
'TV': 6, // Tuvalu
'UG': 9, // Uganda
'UA': 9, // Ukraine
'AE': 9, // United Arab Emirates
'GB': [10,11], // United Kingdom
'UY': 9, // Uruguay
'UZ': 9, // Uzbekistan
'VU': 7, // Vanuatu
'VE': 10, // Venezuela
'VN': [9, 10], // Vietnam (landline: 9, mobile: 10)
'YE': 9, // Yemen
'ZM': 9, // Zambia
'ZW': 9 // Zimbabwe
};
dynamic expectedLength = countryPhoneLengths[countryCode];
if (expectedLength != null) {
if (expectedLength is int) {
return phoneNumber.length == expectedLength;
} else if (expectedLength is List<int>) {
return expectedLength.contains(phoneNumber.length);
}
}
return false;
}