By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – intl-tel-input Display existing number not selecting the relevant flag and instead using the initialCountry flag
JavaScript

Javascript – intl-tel-input Display existing number not selecting the relevant flag and instead using the initialCountry flag

Admin
Last updated: 2023/12/24 at 5:43 AM
Admin
Share
2 Min Read
intl-tel-input Display existing number not selecting the relevant flag and instead using the initialCountry flag

Problem:

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.

Contents
Problem:Solution:

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"
    });

Solution:

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 }}">

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article How to find the version of an installed package from a package-lock.json with lockfileVersion = 3 Javascript – How to find the version of an installed package from a package-lock.json with lockfileVersion = 3
Next Article How to create a html table from array of objects in react? Javascript – How to create a html table from array of objects in react?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?