I am using intl-tel-input to store the full international number of a user in the database. It's working fine but when I want to display that phone number it correctly removes the country code but doesn't select the correct country flag, instead selects the initialCountry value I set.
Here is an example: phone number to store gets correctly saved in database as +3553456546578 But when I want to display it in edit page, it is: from database
I want it to display the correct flag, in this case Albania, with number 3456546578. The value in the input field is: value=" +3553456546578 "
Here is the code I am using:
HTML
<input type="tel" id="mobile_code" class="form-control" placeholder="{{ __('messages.phone') }}" name="phone" value="@if ($customer->phone != 'null') {{ $customer->phone }} @endif">
JS
const input = document.querySelector("#mobile_code");
var phone_number = window.intlTelInput(input, {
preferredCountries: ["us", "eg"],
initialCountry: "eg",
separateDialCode: true,
utilsScript: "https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/11.0.4/js/utils.js"
});
I could reproduce the problem and would consider it a bug, but it's easy to avoid: The value
of the input element must not have a leading whitespace. It would probably be a good idea to trim
the value before saving it to the database but it can also be done inside the blade file or when creating the data for the view.
This is out of my memory and might need adjustment, can't test right now, but it should work:
@php
$customer_phone = $customer->phone != 'null' ? trim($customer->phone) : '';
@endphp
<input type="tel" id="mobile_code" class="form-control" placeholder="{{ __('messages.phone') }}" name="phone" value="{{ $customer_phone }}">