Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put country dial code in intlTelInput in brackets

I'm using intlTelInput on my site. How can I separate dial code using brackets. For ex. default output of this plugin is +1202someNumber and I need (+1)202someNum?

like image 338
Pawel Novikov Avatar asked Mar 23 '17 17:03

Pawel Novikov


People also ask

How do I change the default country code intlTelInput?

Using vanilla JS, you can try as following: var country_code = iti. getSelectedCountryData()["dialCode"]; var number_with_code = iti.

How to get country code in intlTelInput in jQuery?

The getNumber() will give you the country code and number as +919843133490. The setNumber will set the country flag and place the number inn input field.

What is intlTelInput?

It is a jQuery plugin for entering and validating international telephone numbers. It adds a flag dropdown to any input, detects the user's country, displays a relevant placeholder and provides formatting/validation methods. It is also widely known as intl-tel-input.


1 Answers

Based on the docs form here - https://github.com/jackocnr/intl-tel-input - you would need something like this:

var intlNumber = $("#phone").intlTelInput("getNumber"); // get full number eg +17024181234
var countryData = $("#phone").intlTelInput("getSelectedCountryData"); // get country data as obj 

 var countryCode = countryData.dialCode; // using updated doc, code has been replaced with dialCode
countryCode = "+" + countryCode; // convert 1 to +1

var newNo = intlNumber.replace(countryCode, "(" + coountryCode+ ")" ); // final version
like image 75
Manuel Cheța Avatar answered Sep 28 '22 04:09

Manuel Cheța