International telephone input with country flags and dial codes using jQuery
Today we’ll explain to you how to implement international telephone input with country flags and dial codes using jQuery.
It is common functionality to add auto country code in the input field that makes the telephone input field user-friendly.
Using the intl-tel-input
plugin, we can easily display the telephone input field with the flag and code of the respective country. This plugin used to get all the country flags and country code. By IP lookup, the input will detect the user’s country and automatically sets an example number in that country’s format.
You may also like articles related to jQuery.
- Get the number of days between two dates using jQuery
- Create a Modal Popup using jQuery
- Show and Hide a textbox using JavaScript and jQuery
- How to implement jQuery select2 multiple select plugin in PHP
Write the following code and run in the browser to check the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <!DOCTYPE html> <html> <head> <title>International telephone input with country flags and dial codes using jQuery - Clue Mediator</title> <!-- CSS --> <link href='https://maxcdn.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css' rel='stylesheet' type='text/css'> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/css/intlTelInput.min.css" /> <!-- JS --> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/js/intlTelInput.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/intl-tel-input/17.0.3/js/utils.min.js"></script> </head> <body> <div class="container"> <div class="col-md-8 mt-4"> <h5>International telephone input with country flags and dial codes using jQuery - <a href="https://www.cluemediator.com/" target="_blank" rel="noopener noreferrer">Clue Mediator</a></h5> <input type="text" id="phone" /> </div> </div> <script> var input = document.querySelector("#phone"); intlTelInput(input, { initialCountry: "auto", geoIpLookup: function (success, failure) { $.get("https://ipinfo.io", function () { }, "jsonp").always(function (resp) { var countryCode = (resp && resp.country) ? resp.country : "us"; success(countryCode); }); }, }); </script> </body> </html> |
Click here to check more information about the plugin.
Output
That’s it for today.
Thank you for reading. Happy Coding..!! 🙂