unohuim's avatar

Managing Phone Numbers

I don't know what the best practice is for managing phone numbers. Right now I'm storing the numbers as 7 digits and offering 3 separate text fields as form input to restrict the user for simplicity.

.. however, it's always easier coding/validating with one field vs three. I suppose I'll always have to parse out the number when I display it. I'll always want to display the number as 444-444-4444.

Is this good, or are there quality javascripts that can limit the user's input for me and store the number in the db as 444-444-4444?

any thoughts would be appreciated. thanks.

0 likes
11 replies
willvincent's avatar

Probably best to store phone numbers in a text field in the DB, as some countries allow for leading zeros and such.

As for the field, I'd just use a javascript input mask personally. If you use jquery, this plugin is very easy to use for this kind of thing: http://digitalbush.com/projects/masked-input-plugin/

1 like
spekkionu's avatar

Just be aware that limiting phone numbers to a format like this will prevent anyone with a non-US/CA phone number from being able to use the application as well as any number that require an extension.

Depending on your application this may be fine but it helps to be aware of what limitations you are enforcing.

If you want to accept international phone numbers take a look at https://github.com/giggsey/libphonenumber-for-php
It's a php port of google's libphonenumber.
There is a laravel package to help integrate it https://github.com/Propaganistas/Laravel-Phone

2 likes
christopher's avatar

Would also save the Phone number in a text field.

Just think about if a customer comes from another country and uses a country prefix e.g +49 123 213123

opheliadesign's avatar
Level 9

If you will only be dealing with US phone numbers, here's how I do it. I use a regular varchar column (why text?) on the table.

On the model I use a setter to format the phone number (remove all but ten digits) prior to storing it and a getter to return it in this format: 444-444-4444:

public function setPhoneMobileAttribute($phone)
    {
        // Strip all but numbers
        $this->attributes['phone_mobile'] = trim(preg_replace('/^1|\D/', "", $phone));
    }

    public function getPhoneMobileAttribute($phoneNumber)
    {
        if ($phoneNumber) {
            $output = substr($phoneNumber, 0, 3) . '-' . substr($phoneNumber, 3, 3) . '-' . substr($phoneNumber, 6);

            return $output;
        }
    }

On the validation side, I use the following regex. It accepts the phone number in all of the common formats for US numbers, provided that it is a standard ten digit number. So the user can enter it however they wish and at the end it'll be stored as just numbers on the table.

/**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $phoneRegex = "/^(?:(?:\+?1\s*(?:[.-]\s*)?)?(?:\(\s*([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9])\s*\)|([2-9]1[02-9]|[2-9][02-8]1|[2-9][02-8][02-9]))\s*(?:[.-]\s*)?)?([2-9]1[02-9]|[2-9][02-9]1|[2-9][02-9]{2})\s*(?:[.-]\s*)?([0-9]{4})?$/";
        return [
            'first_name' => 'required',
            'last_name' => 'required',
            'email' => ['required', 'email', 'unique:users'],
            'phone_cell' => ['required', "regex:$phoneRegex", 'unique:users'],
            'user_type_id' => 'required',
            'password' => ['required', 'confirmed'],
            'username' => ['required', 'unique:users']
        ];
    }
3 likes
opheliadesign's avatar

@premsaurav try the regex above out. Works great for US numbers.. but guessing that's not what you're dealing with. :)

I mostly work on custom member portals for US companies and organizations, zero chance of an international phone number. Not sure if the OP is dealing with something similar.

opheliadesign's avatar

@Prez Try the regex. Only works for US numbers but accepts just about every way someone would enter a number.

So what I do is validate against that regex and then strip everything but numbers. Working great on several projects.

1 like
willvincent's avatar

@opheliadesign certain varchar is fine. In fact, probably preferable. My point was not to try and cast it to an integer and use an int field in the DB. It would work for US numbers, but not necessarily for non-US numbers.

1 like
unohuim's avatar

wow. thanks for the responses, guys.
in this specific application, i'm strictly interested in US and CAN numbers. I know I said 7 digits, but I meant 10. :)

@opheliadesign that regex eg might just work. The important part is that when I display the number, I can allow the user to call that number with a button press, so having the number stored as 10 digits is nice so that the parsing is predictable. I want every number in the DB to look the same.

opheliadesign's avatar

@unohuim glad I was able to help. :) I try to store numbers as just digits in the DB (varchar) and format them from within the model (see the Getter / Setter) in my example.

I use Twilio often for voice / SMS, this setup has worked beautifully for that application. No problems whatsoever, Twilio can use a number formatted the same way that I prefer to display them - 444-444-4444. Or, if I want to use just digits, I can write a getter like formattedNumber($phoneNumber) which I can call in my views.

For example:

<a href="tel:+{{ $user->phone }}">{{ $user->phone_formatted }}</a>
1 like

Please or to participate in this conversation.