Auto Add Country Code in Input using JavaScript

By | January 10, 2021

Auto Add Country Code in Input field using JavaScript, here are 3 steps to create and add auto country code,

  • Include major CDNs.
  • Create an HTML input field.
  • Include intlTelInput JS to set country code.
  • Create a script to set the code on the selected country.
Auto Add Country Code in Input using JavaScript
Auto Add Country Code in Input using JavaScript

We use International Telephone Input JavaScript plugin to get all the country code with country flag.

Know More: https://github.com/jackocnr/intl-tel-input

Include Major CDNs

I provide all the files and you download complete code on the same at the end of this tutorial.

Create an HTML input field

We use the ID attribute of the phone field to set the country code.

Include intlTelInput JS to set country code

Above is the main code to get the countries from the JavaScript plugin.

Here is the code to set the country code on the same phone field on country change,

Explanation: On this particular code snippet we take the country code from the title using JavaScript replace function.

Here is the complete source code to Auto Add Country Code in Input using JavaScript,

Complete Source Code of Add Country Code in Input field

Click here to check demo,

Here are complete files you can download from the given link,

These are complete solution to add country code on any input field.

Happy Coding..!

Subscribe For Latest Updates

Sign up to best of PHP Code Solutions, informed analysis and opinions on what matters to you.
Invalid email address
We promise not to spam you. You can unsubscribe at any time.

One thought on “Auto Add Country Code in Input using JavaScript

  1. Pingback: Set and Display Session Data in CodeIgniter - PHPCODER.TECH

Leave a Reply

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