Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

phayes0289's avatar

Validating and Storing Telephone Numbers

In my Laravel project, I have a form thta has a telephone number field. It has automatic formatting that disoplays the number as "(xxx) xxx-xxxx" in the form field. When the form gets submitted, it is submitted in that same format. I want to validate the field as a phone number but store it in the database as a 10-digit number.

What does the validation look like, and how can I save it as a 10-digit integer?

0 likes
3 replies
martinbean's avatar
Level 80

It has automatic formatting that disoplays the number as "(xxx) xxx-xxxx"

@phayes0289 And what about phone numbers that aren’t formatting like that?

You should use a service like Twilio that can do a lookup and validate numbers for you. It will then return the E.164 standard formatted number to you (i.e. +1555123456) and also a “nationally” formatted version that you can use for display purposes.

Also, phone numbers are not integers. For example, all numbers here in the UK begin with a zero. If you tried to save a UK phone number in an integer column then it would save incorrectly, as that leading zero would be lost.

phayes0289's avatar

@martinbean

I understand your point, but my project is designed primarily for the United States. Still, I like your idea of validating against Twillo, I will look into that.

And I will definitely change the migrations to make the text field, just in case I pick up an international customer somewhere down the road.

renejp's avatar

Instead of Twilio, I use laravel-phone (github.com/Propaganistas/Laravel-Phone) to validate phone numbers, then I store them in e164 format within my database. When I want to display, then I simply convert to the national or international format, depending on my use-case.

Please or to participate in this conversation.