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?
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.
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.
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.