Skip to content

Commit

Permalink
Merge branch 'master' of github.com:Bluefieldscom/intl-tel-input
Browse files Browse the repository at this point in the history
  • Loading branch information
jackocnr committed Jun 5, 2015
2 parents f46bb7e + ce39e12 commit 69989c5
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 43 deletions.
19 changes: 15 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ A jQuery plugin for entering and validating international telephone numbers. It

<img src="https://raw.github.com/Bluefieldscom/intl-tel-input/master/screenshot.png" width="424px" height="246px">

If you like it, please upvote on [Product Hunt](http://www.producthunt.com/posts/intl-tel-input)!

## Table of Contents

- [Demo and Examples](#demo-and-examples)
Expand Down Expand Up @@ -89,11 +91,20 @@ Add or remove input placeholder with an example number for the selected country.

**defaultCountry**
Type: `String` Default: `""`
Set the default country by it's country code. You can also set it to `"auto"`, which will lookup the user's country based on their IP address - [see example](http://jackocnr.com/lib/intl-tel-input/examples/gen/default-country-ip.html). Otherwise it will just be the first country in the list. _Note that if you choose to do the auto lookup, and you also happen to use the [jquery-cookie](https://github.com/carhartl/jquery-cookie) plugin, it will store the loaded country code in a cookie for future use._
Set the default country by it's country code. You can also set it to `"auto"`, which will lookup the user's country based on their IP address - requires the `geoIpLookup` option - [see example](http://jackocnr.com/lib/intl-tel-input/examples/gen/default-country-ip.html). Otherwise it will just be the first country in the list. _Note that if you choose to do the auto lookup, and you also happen to use the [jquery-cookie](https://github.com/carhartl/jquery-cookie) plugin, it will store the loaded country code in a cookie for future use._

**ipinfoToken**
Type: `String` Default: `""`
When setting `defaultCountry` to `"auto"`, we use a service called [ipinfo](http://ipinfo.io) which requires a special token to be used over https, or if you make >1000 requests/day. Use this option to pass in that token.
**geoIpLookup**
Type: `Function` Default: `null`
When setting `defaultCountry` to `"auto"`, we need to use a special service to lookup the location data for the user. Write a custom method to get the country code. For example if you use [ipinfo.io](http://ipinfo.io/):
```js
geoIpLookup: function(callback) {
$.get('http://ipinfo.io', function() {}, "jsonp").always(function(resp) {
var countryCode = (resp && resp.country) ? resp.country : "";
callback(countryCode);
});
}
```
_Note that the callback must still be called in the event of an error, hence the use of `always` in this example._

**nationalMode**
Type: `Boolean` Default: `true`
Expand Down
33 changes: 15 additions & 18 deletions build/js/intlTelInput.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ https://github.com/Bluefieldscom/intl-tel-input.git
autoHideDialCode: true,
// default country
defaultCountry: "",
// token for ipinfo - required for https or over 1000 daily page views support
ipinfoToken: "",
// geoIp lookup function
geoIpLookup: null,
// don't insert international dial codes
nationalMode: true,
// number type to use for placeholders
Expand Down Expand Up @@ -358,22 +358,19 @@ https://github.com/Bluefieldscom/intl-tel-input.git
} else if (!$.fn[pluginName].startedLoadingAutoCountry) {
// don't do this twice!
$.fn[pluginName].startedLoadingAutoCountry = true;
var ipinfoURL = "//ipinfo.io";
if (this.options.ipinfoToken) {
ipinfoURL += "?token=" + this.options.ipinfoToken;
if (typeof this.options.geoIpLookup === "function") {
this.options.geoIpLookup(function(countryCode) {
$.fn[pluginName].autoCountry = countryCode.toLowerCase();
if ($.cookie) {
$.cookie("itiAutoCountry", $.fn[pluginName].autoCountry, {
path: "/"
});
}
// tell all instances the auto country is ready
// TODO: this should just be the current instances
$(".intl-tel-input input").intlTelInput("autoCountryLoaded");
});
}
// dont bother with the success function arg - instead use always() as should still set a defaultCountry even if the lookup fails
$.get(ipinfoURL, function() {}, "jsonp").always(function(resp) {
$.fn[pluginName].autoCountry = resp && resp.country ? resp.country.toLowerCase() : "";
if ($.cookie) {
$.cookie("itiAutoCountry", $.fn[pluginName].autoCountry, {
path: "/"
});
}
// tell all instances the auto country is ready
// TODO: this should just be the current instances
$(".intl-tel-input input").intlTelInput("autoCountryLoaded");
});
}
},
_initKeyListeners: function() {
Expand Down Expand Up @@ -955,7 +952,7 @@ https://github.com/Bluefieldscom/intl-tel-input.git
/********************
* PUBLIC METHODS
********************/
// this is called when the ipinfo call returns
// this is called when the geoip call returns
autoCountryLoaded: function() {
if (this.options.defaultCountry == "auto") {
this.options.defaultCountry = $.fn[pluginName].autoCountry;
Expand Down
Loading

0 comments on commit 69989c5

Please sign in to comment.