Phone Number Validation with Country Code in Laravel

Phone Number Validation with Country Code in Laravel

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:

CountryAlpha-2 codeNumeric
AfghanistanAF004
AlbaniaAL008
AlgeriaDZ012
American SamoaAS016
AndorraAD020
AngolaAO024
AnguillaAI660
AntarcticaAQ010
Antigua and BarbudaAG028
ArgentinaAR032
ArmeniaAM051
ArubaAW533
AustraliaAU036
AustriaAT040
AzerbaijanAZ031
Bahamas (the)BS044
BahrainBH048
BangladeshBD050
BarbadosBB052
BelarusBY112
BelgiumBE056
BelizeBZ084
BeninBJ204
BermudaBM060
BhutanBT064
Bolivia (Plurinational State of)BO068
Bonaire, Sint Eustatius and SabaBQ535
Bosnia and HerzegovinaBA070
BotswanaBW072
Bouvet IslandBV074
BrazilBR076
British Indian Ocean Territory (the)IO086
Brunei DarussalamBN096
BulgariaBG100
Burkina FasoBF854
BurundiBI108
Cabo VerdeCV132
CambodiaKH116
CameroonCM120
CanadaCA124
Cayman Islands (the)KY136
Central African Republic (the)CF140
ChadTD148
ChileCL152
ChinaCN156
Christmas IslandCX162
Cocos (Keeling) Islands (the)CC166
ColombiaCO170
Comoros (the)KM174
Congo (the Democratic Republic of the)CD180
Congo (the)CG178
Cook Islands (the)CK184
Costa RicaCR188
CroatiaHR191
CubaCU192
CuraçaoCW531
CyprusCY196
CzechiaCZ203
Côte d’IvoireCI384
DenmarkDK208
DjiboutiDJ262
DominicaDM212
Dominican Republic (the)DO214
EcuadorEC218
EgyptEG818
El SalvadorSV222
Equatorial GuineaGQ226
EritreaER232
EstoniaEE233
EswatiniSZ748
EthiopiaET231
Falkland Islands (the) [Malvinas]FK238
Faroe Islands (the)FO234
FijiFJ242
FinlandFI246
FranceFR250
French GuianaGF254
French PolynesiaPF258
French Southern Territories (the)TF260
GabonGA266
Gambia (the)GM270
GeorgiaGE268
GermanyDE276
GhanaGH288
GibraltarGI292
GreeceGR300
GreenlandGL304
GrenadaGD308
GuadeloupeGP312
GuamGU316
GuatemalaGT320
GuernseyGG831
GuineaGN324
Guinea-BissauGW624
GuyanaGY328
HaitiHT332
Heard Island and McDonald IslandsHM334
Holy See (the)VA336
HondurasHN340
Hong KongHK344
HungaryHU348
IcelandIS352
IndiaIN356
IndonesiaID360
Iran (Islamic Republic of)IR364
IraqIQ368
IrelandIE372
Isle of ManIM833
IsraelIL376
ItalyIT380
JamaicaJM388
JapanJP392
JerseyJE832
JordanJO400
KazakhstanKZ398
KenyaKE404
KiribatiKI296
Korea (the Democratic People’s Republic of)KP408
Korea (the Republic of)KR410
KuwaitKW414
KyrgyzstanKG417
Lao People’s Democratic Republic (the)LA418
LatviaLV428
LebanonLB422
LesothoLS426
LiberiaLR430
LibyaLY434
LiechtensteinLI438
LithuaniaLT440
LuxembourgLU442
MacaoMO446
MadagascarMG450
MalawiMW454
MalaysiaMY458
MaldivesMV462
MaliML466
MaltaMT470
Marshall Islands (the)MH584
MartiniqueMQ474
MauritaniaMR478
MauritiusMU480
MayotteYT175
MexicoMX484
Micronesia (Federated States of)FM583
Moldova (the Republic of)MD498
MonacoMC492
MongoliaMN496
MontenegroME499
MontserratMS500
MoroccoMA504
MozambiqueMZ508
MyanmarMM104
NamibiaNA516
NauruNR520
NepalNP524
Netherlands (the)NL528
New CaledoniaNC540
New ZealandNZ554
NicaraguaNI558
Niger (the)NE562
NigeriaNG566
NiueNU570
Norfolk IslandNF574
Northern Mariana Islands (the)MP580
NorwayNO578
OmanOM512
PakistanPK586
PalauPW585
Palestine, State ofPS275
PanamaPA591
Papua New GuineaPG598
ParaguayPY600
PeruPE604
Philippines (the)PH608
PitcairnPN612
PolandPL616
PortugalPT620
Puerto RicoPR630
QatarQA634
Republic of North MacedoniaMK807
RomaniaRO642
Russian Federation (the)RU643
RwandaRW646
RéunionRE638
Saint BarthélemyBL652
Saint Helena, Ascension and Tristan da CunhaSH654
Saint Kitts and NevisKN659
Saint LuciaLC662
Saint Martin (French part)MF663
Saint Pierre and MiquelonPM666
Saint Vincent and the GrenadinesVC670
SamoaWS882
San MarinoSM674
Sao Tome and PrincipeST678
Saudi ArabiaSA682
SenegalSN686
SerbiaRS688
SeychellesSC690
Sierra LeoneSL694
SingaporeSG702
Sint Maarten (Dutch part)SX534
SlovakiaSK703
SloveniaSI705
Solomon IslandsSB090
SomaliaSO706
South AfricaZA710
South Georgia and the South Sandwich IslandsGS239
South SudanSS728
SpainES724
Sri LankaLK144
Sudan (the)SD729
SurinameSR740
Svalbard and Jan MayenSJ744
SwedenSE752
SwitzerlandCH756
Syrian Arab RepublicSY760
Taiwan (Province of China)TW158
TajikistanTJ762
Tanzania, United Republic ofTZ834
ThailandTH764
Timor-LesteTL626
TogoTG768
TokelauTK772
TongaTO776
Trinidad and TobagoTT780
TunisiaTN788
TurkeyTR792
TurkmenistanTM795
Turks and Caicos Islands (the)TC796
TuvaluTV798
UgandaUG800
UkraineUA804
United Arab Emirates (the)AE784
United Kingdom of Great Britain and Northern Ireland (the)GB826
United States Minor Outlying Islands (the)UM581
United States of America (the)US840
UruguayUY858
UzbekistanUZ860
VanuatuVU548
Venezuela (Bolivarian Republic of)VE862
Viet NamVN704
Virgin Islands (British)VG092
Virgin Islands (U.S.)VI850
Wallis and FutunaWF876
Western SaharaEH732
YemenYE887
ZambiaZM894
ZimbabweZW716
Åland IslandsAX248

Conclusion

Congratulation! you have learned how to validate international mobile or phone number in laravel with country code.

Recommended Tutorials

AuthorAdmin

Greetings, I'm Devendra Dode, a full-stack developer, entrepreneur, and the proud owner of Tutsmake.com. My passion lies in crafting informative tutorials and offering valuable tips to assist fellow developers on their coding journey. Within my content, I cover a spectrum of technologies, including PHP, Python, JavaScript, jQuery, Laravel, Livewire, CodeIgniter, Node.js, Express.js, Vue.js, Angular.js, React.js, MySQL, MongoDB, REST APIs, Windows, XAMPP, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL, and Bootstrap. Whether you're starting out or looking for advanced examples, I provide step-by-step guides and practical demonstrations to make your learning experience seamless. Let's explore the diverse realms of coding together.

Leave a Reply

Your email address will not be published. Required fields are marked *