Phone and mobile number formats are different for each country, as you know they have different country codes. To validate phone or mobile number in Laravel with country code; simply install composer require propaganistas/laravel-phone
package and use it with validation rules into your Laravel project.
The composer require propaganistas/laravel-phone
package will allow validating international phone or mobile number according to the country code given in the validation rules, for example, you want to validate United States and United Kingdom phone numbers, simply use 'phone_number' => 'required|phone:US,UK'
, this will validate the phone number in Laravel with the country code.
How to Validate a Mobile or Phone Number in Laravel
Steps to validate mobile or phone number with international country codes like UK, US, IN, CA, EU, etc in Laravel application:
Step 1: Install Laravel Phone Package
Simply open your cmd or terminal window and run the abc command into it to install phone number validation package in your laravel application:
composer require propaganistas/laravel-phone
Step 2: Add Fillable Property in Model
Navigate to app/models folder, and open your model class, and then add phone or mobile number fillable property in it like the following:
class Your_model extends Model { protected $fillable = [ 'phone_number', ]; }
Step 3: Validate Phone Number Laravel
Every country has a standard format of phone and mobile number, you can validate it according to the phone or mobile number format of the country, For this, you have to use country codes like US, UK, IN, CA,EU, etc, with validation in rules on Laravel methods.
For example, if you want to validate the phone number of the Europe country France in Laravel, you can simply validate it by using the country code (FR) like the following:
$request->validate([ // Allow only phone numbers from the FR 'phone_number' => 'required|phone:FR', ]);
To add your custom error message with phone number verification; You can do something like this:
$request->validate([ // Allow only phone numbers from the France 'phone_number' => 'required|phone:FR', ], [ // Set a custom validation error message 'phone_number.phone' => 'The phone number must be a valid france phone number.', ]);
Step 4: Display Phone Number Validation Message
When you validate a phone or mobile number, you also have to display a validation message to let you know whether the user has provided the correct phone or mobile number or not in application.
Simply open your blade view file, and use the following code to display phone or mobile number validation messages in laravel:
@error('phone_number') <div class="text-danger"> <small>{{ $message }}</small> </div> @enderror
Here are list of country codes, and you can use the country code of the country whose mobile or phone number you want to verify by finding it in the list:
Country | Alpha-2 code | Numeric |
---|---|---|
Afghanistan | AF | 004 |
Albania | AL | 008 |
Algeria | DZ | 012 |
American Samoa | AS | 016 |
Andorra | AD | 020 |
Angola | AO | 024 |
Anguilla | AI | 660 |
Antarctica | AQ | 010 |
Antigua and Barbuda | AG | 028 |
Argentina | AR | 032 |
Armenia | AM | 051 |
Aruba | AW | 533 |
Australia | AU | 036 |
Austria | AT | 040 |
Azerbaijan | AZ | 031 |
Bahamas (the) | BS | 044 |
Bahrain | BH | 048 |
Bangladesh | BD | 050 |
Barbados | BB | 052 |
Belarus | BY | 112 |
Belgium | BE | 056 |
Belize | BZ | 084 |
Benin | BJ | 204 |
Bermuda | BM | 060 |
Bhutan | BT | 064 |
Bolivia (Plurinational State of) | BO | 068 |
Bonaire, Sint Eustatius and Saba | BQ | 535 |
Bosnia and Herzegovina | BA | 070 |
Botswana | BW | 072 |
Bouvet Island | BV | 074 |
Brazil | BR | 076 |
British Indian Ocean Territory (the) | IO | 086 |
Brunei Darussalam | BN | 096 |
Bulgaria | BG | 100 |
Burkina Faso | BF | 854 |
Burundi | BI | 108 |
Cabo Verde | CV | 132 |
Cambodia | KH | 116 |
Cameroon | CM | 120 |
Canada | CA | 124 |
Cayman Islands (the) | KY | 136 |
Central African Republic (the) | CF | 140 |
Chad | TD | 148 |
Chile | CL | 152 |
China | CN | 156 |
Christmas Island | CX | 162 |
Cocos (Keeling) Islands (the) | CC | 166 |
Colombia | CO | 170 |
Comoros (the) | KM | 174 |
Congo (the Democratic Republic of the) | CD | 180 |
Congo (the) | CG | 178 |
Cook Islands (the) | CK | 184 |
Costa Rica | CR | 188 |
Croatia | HR | 191 |
Cuba | CU | 192 |
Curaçao | CW | 531 |
Cyprus | CY | 196 |
Czechia | CZ | 203 |
Côte d’Ivoire | CI | 384 |
Denmark | DK | 208 |
Djibouti | DJ | 262 |
Dominica | DM | 212 |
Dominican Republic (the) | DO | 214 |
Ecuador | EC | 218 |
Egypt | EG | 818 |
El Salvador | SV | 222 |
Equatorial Guinea | GQ | 226 |
Eritrea | ER | 232 |
Estonia | EE | 233 |
Eswatini | SZ | 748 |
Ethiopia | ET | 231 |
Falkland Islands (the) [Malvinas] | FK | 238 |
Faroe Islands (the) | FO | 234 |
Fiji | FJ | 242 |
Finland | FI | 246 |
France | FR | 250 |
French Guiana | GF | 254 |
French Polynesia | PF | 258 |
French Southern Territories (the) | TF | 260 |
Gabon | GA | 266 |
Gambia (the) | GM | 270 |
Georgia | GE | 268 |
Germany | DE | 276 |
Ghana | GH | 288 |
Gibraltar | GI | 292 |
Greece | GR | 300 |
Greenland | GL | 304 |
Grenada | GD | 308 |
Guadeloupe | GP | 312 |
Guam | GU | 316 |
Guatemala | GT | 320 |
Guernsey | GG | 831 |
Guinea | GN | 324 |
Guinea-Bissau | GW | 624 |
Guyana | GY | 328 |
Haiti | HT | 332 |
Heard Island and McDonald Islands | HM | 334 |
Holy See (the) | VA | 336 |
Honduras | HN | 340 |
Hong Kong | HK | 344 |
Hungary | HU | 348 |
Iceland | IS | 352 |
India | IN | 356 |
Indonesia | ID | 360 |
Iran (Islamic Republic of) | IR | 364 |
Iraq | IQ | 368 |
Ireland | IE | 372 |
Isle of Man | IM | 833 |
Israel | IL | 376 |
Italy | IT | 380 |
Jamaica | JM | 388 |
Japan | JP | 392 |
Jersey | JE | 832 |
Jordan | JO | 400 |
Kazakhstan | KZ | 398 |
Kenya | KE | 404 |
Kiribati | KI | 296 |
Korea (the Democratic People’s Republic of) | KP | 408 |
Korea (the Republic of) | KR | 410 |
Kuwait | KW | 414 |
Kyrgyzstan | KG | 417 |
Lao People’s Democratic Republic (the) | LA | 418 |
Latvia | LV | 428 |
Lebanon | LB | 422 |
Lesotho | LS | 426 |
Liberia | LR | 430 |
Libya | LY | 434 |
Liechtenstein | LI | 438 |
Lithuania | LT | 440 |
Luxembourg | LU | 442 |
Macao | MO | 446 |
Madagascar | MG | 450 |
Malawi | MW | 454 |
Malaysia | MY | 458 |
Maldives | MV | 462 |
Mali | ML | 466 |
Malta | MT | 470 |
Marshall Islands (the) | MH | 584 |
Martinique | MQ | 474 |
Mauritania | MR | 478 |
Mauritius | MU | 480 |
Mayotte | YT | 175 |
Mexico | MX | 484 |
Micronesia (Federated States of) | FM | 583 |
Moldova (the Republic of) | MD | 498 |
Monaco | MC | 492 |
Mongolia | MN | 496 |
Montenegro | ME | 499 |
Montserrat | MS | 500 |
Morocco | MA | 504 |
Mozambique | MZ | 508 |
Myanmar | MM | 104 |
Namibia | NA | 516 |
Nauru | NR | 520 |
Nepal | NP | 524 |
Netherlands (the ) | NL | 528 |
New Caledonia | NC | 540 |
New Zealand | NZ | 554 |
Nicaragua | NI | 558 |
Niger (the) | NE | 562 |
Nigeria | NG | 566 |
Niue | NU | 570 |
Norfolk Island | NF | 574 |
Northern Mariana Islands (the) | MP | 580 |
Norway | NO | 578 |
Oman | OM | 512 |
Pakistan | PK | 586 |
Palau | PW | 585 |
Palestine, State of | PS | 275 |
Panama | PA | 591 |
Papua New Guinea | PG | 598 |
Paraguay | PY | 600 |
Peru | PE | 604 |
Philippines (the) | PH | 608 |
Pitcairn | PN | 612 |
Poland | PL | 616 |
Portugal | PT | 620 |
Puerto Rico | PR | 630 |
Qatar | QA | 634 |
Republic of North Macedonia | MK | 807 |
Romania | RO | 642 |
Russian Federation (the) | RU | 643 |
Rwanda | RW | 646 |
Réunion | RE | 638 |
Saint Barthélemy | BL | 652 |
Saint Helena, Ascension and Tristan da Cunha | SH | 654 |
Saint Kitts and Nevis | KN | 659 |
Saint Lucia | LC | 662 |
Saint Martin (French part) | MF | 663 |
Saint Pierre and Miquelon | PM | 666 |
Saint Vincent and the Grenadines | VC | 670 |
Samoa | WS | 882 |
San Marino | SM | 674 |
Sao Tome and Principe | ST | 678 |
Saudi Arabia | SA | 682 |
Senegal | SN | 686 |
Serbia | RS | 688 |
Seychelles | SC | 690 |
Sierra Leone | SL | 694 |
Singapore | SG | 702 |
Sint Maarten (Dutch part) | SX | 534 |
Slovakia | SK | 703 |
Slovenia | SI | 705 |
Solomon Islands | SB | 090 |
Somalia | SO | 706 |
South Africa | ZA | 710 |
South Georgia and the South Sandwich Islands | GS | 239 |
South Sudan | SS | 728 |
Spain | ES | 724 |
Sri Lanka | LK | 144 |
Sudan (the) | SD | 729 |
Suriname | SR | 740 |
Svalbard and Jan Mayen | SJ | 744 |
Sweden | SE | 752 |
Switzerland | CH | 756 |
Syrian Arab Republic | SY | 760 |
Taiwan (Province of China) | TW | 158 |
Tajikistan | TJ | 762 |
Tanzania, United Republic of | TZ | 834 |
Thailand | TH | 764 |
Timor-Leste | TL | 626 |
Togo | TG | 768 |
Tokelau | TK | 772 |
Tonga | TO | 776 |
Trinidad and Tobago | TT | 780 |
Tunisia | TN | 788 |
Turkey | TR | 792 |
Turkmenistan | TM | 795 |
Turks and Caicos Islands (the) | TC | 796 |
Tuvalu | TV | 798 |
Uganda | UG | 800 |
Ukraine | UA | 804 |
United Arab Emirates (the) | AE | 784 |
United Kingdom of Great Britain and Northern Ireland (the) | GB | 826 |
United States Minor Outlying Islands (the) | UM | 581 |
United States of America (the) | US | 840 |
Uruguay | UY | 858 |
Uzbekistan | UZ | 860 |
Vanuatu | VU | 548 |
Venezuela (Bolivarian Republic of) | VE | 862 |
Viet Nam | VN | 704 |
Virgin Islands (British) | VG | 092 |
Virgin Islands (U.S.) | VI | 850 |
Wallis and Futuna | WF | 876 |
Western Sahara | EH | 732 |
Yemen | YE | 887 |
Zambia | ZM | 894 |
Zimbabwe | ZW | 716 |
Åland Islands | AX | 248 |
Conclusion
Congratulation! you have learned how to validate international mobile or phone number in laravel with country code.