phone format making format wrong
Why phone mask is taking out one digit from data.... My contact was this before 555-579-4532 and I applied mask .. now it is like this +1 (555) 579-453
->mask(RawJs::make(<<<'JS'
'+1 (999) 999-9999'
JS));
5 Replies
Well, that's the format you've asked for. When using a mask, anything that isn't a wildcard character (*, a and 9) in your mask just gets passed through as part of the field state.
Best practice for phone numbers is to always store them in a normalized format, like E164, then display them in your desired format (like National).
If you look here, you'll find where I gave examples of how to do this, using the brick/phonenumber composer package.
An example of a form field using this technique would be ...
Oops, forgot I'd posted that in a closed channel. I'll re-post here ...
My StringHelper looks like this ... (my dfpc.phone.region is just 'US')
It uses this composer package ...
https://github.com/brick/phonenumber
GitHub
GitHub - brick/phonenumber: A phone number library for PHP
A phone number library for PHP. Contribute to brick/phonenumber development by creating an account on GitHub.
And a column using it might look like this ... it uses formatStateUsing to format the number, and a custom query on the searchable() to ignore formatted characters when searching.
If you've already got phone numbers stored non-normalized, you'd have to whip up a little script to normalize them in place. Here's an Artisan command I wrote to do that for my tables, obviously you'd have to tweak it with your tables / models / field name(s) ...
End result ... phone numbers are always stored in the database normalized to E164, like +12345551212, and formatted to your desired format when displayed / masked.
great thanks 🙂
It looks like a lot of work, but once you've set it up, it really isn't. Just remember to format/dehydrate your phone fields.