Auto Add Country Code in Input using JavaScript

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 will use the International Telephone Input JavaScript plugin to get all the country codes with country flags.

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 will 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 the Input field

Click here to check the demo,

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

https://codingtasks.net/download/add-country-code-in-input-using-javascript-complete-source-code-files/

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

Happy Coding..!

Was this article helpful?
YesNo

By Bikash

My name is Bikash Kr. Panda. I own and operate PHPCODER.TECH. I am a web Programmer by profession and working on more than 50 projects to date. Currently I am working on the web-based project and all the CMS and frameworks which are based on PHP.

6 thoughts on “Auto Add Country Code in Input using JavaScript”

Leave a Reply

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