From 963de5a6e577e1502425d458673543f4389b1b86 Mon Sep 17 00:00:00 2001 From: Jack OConnor Date: Sat, 6 Sep 2014 18:35:47 -0700 Subject: [PATCH] fix autoFormat when nationalMode enabled --- build/js/intlTelInput.js | 26 ++-- build/js/intlTelInput.min.js | 2 +- lib/libphonenumber/build/utils.js | 199 +++++++++++++++--------------- lib/libphonenumber/src/utils.js | 19 +-- src/js/intlTelInput.js | 26 ++-- 5 files changed, 136 insertions(+), 136 deletions(-) diff --git a/build/js/intlTelInput.js b/build/js/intlTelInput.js index f50c1dff6..8679143e5 100644 --- a/build/js/intlTelInput.js +++ b/build/js/intlTelInput.js @@ -69,7 +69,7 @@ https://github.com/Bluefieldscom/intl-tel-input.git init: function() { // if in nationalMode, disable options relating to dial codes if (this.options.nationalMode) { - this.options.autoFormat = this.options.autoHideDialCode = false; + this.options.autoHideDialCode = false; } // chrome on android has issues with key events // backspace issues for inputs with type=text: https://code.google.com/p/chromium/issues/detail?id=184812 @@ -280,14 +280,16 @@ https://github.com/Bluefieldscom/intl-tel-input.git var addSuffix = !(e.which == keys.BSPACE && cursorAtEnd); that._handleInputKey(null, addSuffix); } - // prevent deleting the plus - var val = that.telInput.val(); - if (val.substr(0, 1) != "+") { - // newCursorPos is current pos + 1 to account for the plus we are about to add - var newCursorPos = that.isGoodBrowser ? input.selectionStart + 1 : 0; - that.telInput.val("+" + val); - if (that.isGoodBrowser) { - input.setSelectionRange(newCursorPos, newCursorPos); + // prevent deleting the plus (if not in nationalMode) + if (!that.options.nationalMode) { + var val = that.telInput.val(); + if (val.substr(0, 1) != "+") { + // newCursorPos is current pos + 1 to account for the plus we are about to add + var newCursorPos = that.isGoodBrowser ? input.selectionStart + 1 : 0; + that.telInput.val("+" + val); + if (that.isGoodBrowser) { + input.setSelectionRange(newCursorPos, newCursorPos); + } } } } else { @@ -537,11 +539,7 @@ https://github.com/Bluefieldscom/intl-tel-input.git _updateVal: function(val, addSuffix) { var formatted; if (this.options.autoFormat && window.intlTelInputUtils) { - // don't try to add the suffix if we dont have a full dial code - if (!this._getDialCode(val)) { - addSuffix = false; - } - formatted = intlTelInputUtils.formatNumber(val, addSuffix); + formatted = intlTelInputUtils.formatNumber(val, this.selectedCountryData.iso2, addSuffix); } else { // no autoFormat, so just insert the original value formatted = val; diff --git a/build/js/intlTelInput.min.js b/build/js/intlTelInput.min.js index 40ae75e02..54f948d01 100644 --- a/build/js/intlTelInput.min.js +++ b/build/js/intlTelInput.min.js @@ -2,4 +2,4 @@ International Telephone Input v2.0.10 https://github.com/Bluefieldscom/intl-tel-input.git */ -!function(a){"function"==typeof define&&define.amd?define(["jquery"],function(b){a(b,window,document)}):a(jQuery,window,document)}(function(a,b,c,d){"use strict";function e(b,c){this.element=b,this.options=a.extend({},h,c),this._defaults=h,this.ns="."+f+g++,this.isGoodBrowser=Boolean(b.setSelectionRange),this.hadInitialPlaceholder=Boolean(a(b).attr("placeholder")),this._name=f,this.init()}var f="intlTelInput",g=1,h={autoFormat:!1,autoHideDialCode:!0,defaultCountry:"",nationalMode:!1,onlyCountries:[],preferredCountries:["us","gb"],responsiveDropdown:!1,utilsScript:""},i={UP:38,DOWN:40,ENTER:13,ESC:27,PLUS:43,A:65,Z:90,ZERO:48,NINE:57,SPACE:32,BSPACE:8,DEL:46,CTRL:17,CMD1:91,CMD2:224},j=!1;a(b).load(function(){j=!0}),e.prototype={init:function(){this.options.nationalMode&&(this.options.autoFormat=this.options.autoHideDialCode=!1),navigator.userAgent.match(/Android/i)&&navigator.userAgent.match(/Chrome/i)&&(this.options.autoFormat=!1),this._processCountryData(),this._generateMarkup(),this._setInitialState(),this._initListeners()},_processCountryData:function(){this._setInstanceCountryData(),this._setPreferredCountries()},_addCountryCode:function(a,b,c){b in this.countryCodes||(this.countryCodes[b]=[]);var d=c||0;this.countryCodes[b][d]=a},_setInstanceCountryData:function(){var a;if(this.options.onlyCountries.length)for(this.countries=[],a=0;a",{"class":"intl-tel-input"}));var b=a("
",{"class":"flag-dropdown"}).insertAfter(this.telInput),c=a("
",{"class":"selected-flag"}).appendTo(b);this.selectedFlagInner=a("
",{"class":"flag"}).appendTo(c),a("
",{"class":"arrow"}).appendTo(this.selectedFlagInner),this.countryList=a("
    ",{"class":"country-list v-hide"}).appendTo(b),this.preferredCountries.length&&(this._appendListItems(this.preferredCountries,"preferred"),a("
  • ",{"class":"divider"}).appendTo(this.countryList)),this._appendListItems(this.countries,""),this.dropdownHeight=this.countryList.outerHeight(),this.countryList.removeClass("v-hide").addClass("hide"),this.options.responsiveDropdown&&this.countryList.outerWidth(this.telInput.outerWidth()),this.countryListItems=this.countryList.children(".country")},_appendListItems:function(a,b){for(var c="",d=0;d",c+="
    ",c+=""+e.name+"",c+="+"+e.dialCode+"",c+="
  • "}this.countryList.append(c)},_setInitialState:function(){var a=this.telInput.val();if(!a||!this.setNumber(a)){var b;b=this.options.defaultCountry?this._getCountryData(this.options.defaultCountry,!1,!1):this.preferredCountries.length?this.preferredCountries[0]:this.countries[0],this._selectFlag(b.iso2),a||this.options.autoHideDialCode||this._resetToDialCode(b.dialCode)}},_initListeners:function(){var c=this;this.options.autoHideDialCode&&this._initAutoHideDialCode();var d=this.telInput.closest("label");d.length&&d.on("click"+this.ns,function(a){c.countryList.hasClass("hide")?c.telInput.focus():a.preventDefault()}),this.options.autoFormat&&this.telInput.on("keypress"+this.ns,function(a){if(a.which>=i.SPACE){a.preventDefault();var b=a.which>=i.ZERO&&a.which<=i.NINE,d=c.telInput[0],e=c.isGoodBrowser&&d.selectionStart==d.selectionEnd;if(b||e){var f=b?String.fromCharCode(a.which):null;c._handleInputKey(f,!0)}}}),this.telInput.on("keyup"+this.ns,function(a){if(c.options.autoFormat){var b=a.which==i.CTRL||a.which==i.CMD1||a.which==i.CMD2,d=c.telInput[0],e=c.isGoodBrowser&&d.selectionStart==d.selectionEnd,f=c.isGoodBrowser&&d.selectionStart==c.telInput.val().length;if(a.which==i.DEL||a.which==i.BSPACE||b&&e){var g=!(a.which==i.BSPACE&&f);c._handleInputKey(null,g)}var h=c.telInput.val();if("+"!=h.substr(0,1)){var j=c.isGoodBrowser?d.selectionStart+1:0;c.telInput.val("+"+h),c.isGoodBrowser&&d.setSelectionRange(j,j)}}else c._updateFlag(c.telInput.val())});var e=this.selectedFlagInner.parent();if(e.on("click"+this.ns,function(){c.countryList.hasClass("hide")&&!c.telInput.prop("disabled")&&c._showDropdown()}),this.options.utilsScript&&!a.fn[f].injectedUtilsScript){a.fn[f].injectedUtilsScript=!0;var g=function(){a.getScript(c.options.utilsScript,function(){a(".intl-tel-input input").intlTelInput("utilsLoaded")})};j?g():a(b).load(g)}},_handleInputKey:function(a,b){var c=this.telInput.val(),d=null,e=!1,f=this.telInput[0];if(this.isGoodBrowser){var g=f.selectionEnd,h=c.length;e=g==h,a?(c=c.substring(0,f.selectionStart)+a+c.substring(g,h),e||(d=g+(c.length-h))):d=f.selectionStart}else a&&(c+=a);this.setNumber(c,b),this.isGoodBrowser&&(e&&(d=this.telInput.val().length),f.setSelectionRange(d,d))},_initAutoHideDialCode:function(){var a=this;this.telInput.on("mousedown"+this.ns,function(b){a.telInput.is(":focus")||a.telInput.val()||(b.preventDefault(),a.telInput.focus())}),this.telInput.on("focus"+this.ns,function(){a.telInput.val()||(a._updateVal("+"+a.selectedCountryData.dialCode,!0),a.telInput.one("keypress.plus"+a.ns,function(b){b.which==i.PLUS&&a.telInput.val("+")}),setTimeout(function(){a._cursorToEnd()}))}),this.telInput.on("blur"+this.ns,function(){var b=a.telInput.val(),c="+"==b.substr(0,1);if(c){var d=a._getNumeric(b);d&&a.selectedCountryData.dialCode!=d||a.telInput.val("")}a.telInput.off("keypress.plus"+a.ns)})},_getNumeric:function(a){return a.replace(/\D/g,"")},_cursorToEnd:function(){var a=this.telInput[0];if(this.isGoodBrowser){var b=this.telInput.val().length;a.setSelectionRange(b,b)}},_showDropdown:function(){this._setDropdownPosition();var a=this.countryList.children(".active");this._highlightListItem(a),this.countryList.removeClass("hide"),this._scrollTo(a),this._bindDropdownListeners(),this.selectedFlagInner.children(".arrow").addClass("up")},_setDropdownPosition:function(){var c=this.telInput.offset().top,d=a(b).scrollTop(),e=c+this.telInput.outerHeight()+this.dropdownHeightd,g=!e&&f?"-"+(this.dropdownHeight-1)+"px":"";this.countryList.css("top",g)},_bindDropdownListeners:function(){var b=this;this.countryList.on("mouseover"+this.ns,".country",function(){b._highlightListItem(a(this))}),this.countryList.on("click"+this.ns,".country",function(){b._selectListItem(a(this))});var d=!0;a("html").on("click"+this.ns,function(){d||b._closeDropdown(),d=!1});var e="",f=null;a(c).on("keydown"+this.ns,function(a){a.preventDefault(),a.which==i.UP||a.which==i.DOWN?b._handleUpDownKey(a.which):a.which==i.ENTER?b._handleEnterKey():a.which==i.ESC?b._closeDropdown():(a.which>=i.A&&a.which<=i.Z||a.which==i.SPACE)&&(f&&clearTimeout(f),e+=String.fromCharCode(a.which),b._searchForCountry(e),f=setTimeout(function(){e=""},1e3))})},_handleUpDownKey:function(a){var b=this.countryList.children(".highlight").first(),c=a==i.UP?b.prev():b.next();c.length&&(c.hasClass("divider")&&(c=a==i.UP?c.prev():c.next()),this._highlightListItem(c),this._scrollTo(c))},_handleEnterKey:function(){var a=this.countryList.children(".highlight").first();a.length&&this._selectListItem(a)},_searchForCountry:function(a){for(var b=0;b=4},_resetToDialCode:function(a){var b=this.options.nationalMode?"":"+"+a;this.telInput.val(b)},_highlightListItem:function(a){this.countryListItems.removeClass("highlight"),a.addClass("highlight")},_getCountryData:function(a,b,c){for(var d=b?k:this.countries,e=0;eh)b&&(j-=k),c.scrollTop(j);else if(i>f){b&&(j+=k);var l=d-g;c.scrollTop(j-l)}},_updateDialCode:function(b){var c,d=this.telInput.val(),e=this._getDialCode(d);if(e.length>1)c=d.replace(e,b);else{var f=d&&"+"!=d.substr(0,1)?a.trim(d):"";c=b+f}this._updateVal(c,!0)},_getDialCode:function(b){var c="";if("+"==b.charAt(0))for(var d="",e=0;e",{"class":"intl-tel-input"}));var b=a("
    ",{"class":"flag-dropdown"}).insertAfter(this.telInput),c=a("
    ",{"class":"selected-flag"}).appendTo(b);this.selectedFlagInner=a("
    ",{"class":"flag"}).appendTo(c),a("
    ",{"class":"arrow"}).appendTo(this.selectedFlagInner),this.countryList=a("
      ",{"class":"country-list v-hide"}).appendTo(b),this.preferredCountries.length&&(this._appendListItems(this.preferredCountries,"preferred"),a("
    • ",{"class":"divider"}).appendTo(this.countryList)),this._appendListItems(this.countries,""),this.dropdownHeight=this.countryList.outerHeight(),this.countryList.removeClass("v-hide").addClass("hide"),this.options.responsiveDropdown&&this.countryList.outerWidth(this.telInput.outerWidth()),this.countryListItems=this.countryList.children(".country")},_appendListItems:function(a,b){for(var c="",d=0;d",c+="
      ",c+=""+e.name+"",c+="+"+e.dialCode+"",c+="
    • "}this.countryList.append(c)},_setInitialState:function(){var a=this.telInput.val();if(!a||!this.setNumber(a)){var b;b=this.options.defaultCountry?this._getCountryData(this.options.defaultCountry,!1,!1):this.preferredCountries.length?this.preferredCountries[0]:this.countries[0],this._selectFlag(b.iso2),a||this.options.autoHideDialCode||this._resetToDialCode(b.dialCode)}},_initListeners:function(){var c=this;this.options.autoHideDialCode&&this._initAutoHideDialCode();var d=this.telInput.closest("label");d.length&&d.on("click"+this.ns,function(a){c.countryList.hasClass("hide")?c.telInput.focus():a.preventDefault()}),this.options.autoFormat&&this.telInput.on("keypress"+this.ns,function(a){if(a.which>=i.SPACE){a.preventDefault();var b=a.which>=i.ZERO&&a.which<=i.NINE,d=c.telInput[0],e=c.isGoodBrowser&&d.selectionStart==d.selectionEnd;if(b||e){var f=b?String.fromCharCode(a.which):null;c._handleInputKey(f,!0)}}}),this.telInput.on("keyup"+this.ns,function(a){if(c.options.autoFormat){var b=a.which==i.CTRL||a.which==i.CMD1||a.which==i.CMD2,d=c.telInput[0],e=c.isGoodBrowser&&d.selectionStart==d.selectionEnd,f=c.isGoodBrowser&&d.selectionStart==c.telInput.val().length;if(a.which==i.DEL||a.which==i.BSPACE||b&&e){var g=!(a.which==i.BSPACE&&f);c._handleInputKey(null,g)}if(!c.options.nationalMode){var h=c.telInput.val();if("+"!=h.substr(0,1)){var j=c.isGoodBrowser?d.selectionStart+1:0;c.telInput.val("+"+h),c.isGoodBrowser&&d.setSelectionRange(j,j)}}}else c._updateFlag(c.telInput.val())});var e=this.selectedFlagInner.parent();if(e.on("click"+this.ns,function(){c.countryList.hasClass("hide")&&!c.telInput.prop("disabled")&&c._showDropdown()}),this.options.utilsScript&&!a.fn[f].injectedUtilsScript){a.fn[f].injectedUtilsScript=!0;var g=function(){a.getScript(c.options.utilsScript,function(){a(".intl-tel-input input").intlTelInput("utilsLoaded")})};j?g():a(b).load(g)}},_handleInputKey:function(a,b){var c=this.telInput.val(),d=null,e=!1,f=this.telInput[0];if(this.isGoodBrowser){var g=f.selectionEnd,h=c.length;e=g==h,a?(c=c.substring(0,f.selectionStart)+a+c.substring(g,h),e||(d=g+(c.length-h))):d=f.selectionStart}else a&&(c+=a);this.setNumber(c,b),this.isGoodBrowser&&(e&&(d=this.telInput.val().length),f.setSelectionRange(d,d))},_initAutoHideDialCode:function(){var a=this;this.telInput.on("mousedown"+this.ns,function(b){a.telInput.is(":focus")||a.telInput.val()||(b.preventDefault(),a.telInput.focus())}),this.telInput.on("focus"+this.ns,function(){a.telInput.val()||(a._updateVal("+"+a.selectedCountryData.dialCode,!0),a.telInput.one("keypress.plus"+a.ns,function(b){b.which==i.PLUS&&a.telInput.val("+")}),setTimeout(function(){a._cursorToEnd()}))}),this.telInput.on("blur"+this.ns,function(){var b=a.telInput.val(),c="+"==b.substr(0,1);if(c){var d=a._getNumeric(b);d&&a.selectedCountryData.dialCode!=d||a.telInput.val("")}a.telInput.off("keypress.plus"+a.ns)})},_getNumeric:function(a){return a.replace(/\D/g,"")},_cursorToEnd:function(){var a=this.telInput[0];if(this.isGoodBrowser){var b=this.telInput.val().length;a.setSelectionRange(b,b)}},_showDropdown:function(){this._setDropdownPosition();var a=this.countryList.children(".active");this._highlightListItem(a),this.countryList.removeClass("hide"),this._scrollTo(a),this._bindDropdownListeners(),this.selectedFlagInner.children(".arrow").addClass("up")},_setDropdownPosition:function(){var c=this.telInput.offset().top,d=a(b).scrollTop(),e=c+this.telInput.outerHeight()+this.dropdownHeightd,g=!e&&f?"-"+(this.dropdownHeight-1)+"px":"";this.countryList.css("top",g)},_bindDropdownListeners:function(){var b=this;this.countryList.on("mouseover"+this.ns,".country",function(){b._highlightListItem(a(this))}),this.countryList.on("click"+this.ns,".country",function(){b._selectListItem(a(this))});var d=!0;a("html").on("click"+this.ns,function(){d||b._closeDropdown(),d=!1});var e="",f=null;a(c).on("keydown"+this.ns,function(a){a.preventDefault(),a.which==i.UP||a.which==i.DOWN?b._handleUpDownKey(a.which):a.which==i.ENTER?b._handleEnterKey():a.which==i.ESC?b._closeDropdown():(a.which>=i.A&&a.which<=i.Z||a.which==i.SPACE)&&(f&&clearTimeout(f),e+=String.fromCharCode(a.which),b._searchForCountry(e),f=setTimeout(function(){e=""},1e3))})},_handleUpDownKey:function(a){var b=this.countryList.children(".highlight").first(),c=a==i.UP?b.prev():b.next();c.length&&(c.hasClass("divider")&&(c=a==i.UP?c.prev():c.next()),this._highlightListItem(c),this._scrollTo(c))},_handleEnterKey:function(){var a=this.countryList.children(".highlight").first();a.length&&this._selectListItem(a)},_searchForCountry:function(a){for(var b=0;b=4},_resetToDialCode:function(a){var b=this.options.nationalMode?"":"+"+a;this.telInput.val(b)},_highlightListItem:function(a){this.countryListItems.removeClass("highlight"),a.addClass("highlight")},_getCountryData:function(a,b,c){for(var d=b?k:this.countries,e=0;eh)b&&(j-=k),c.scrollTop(j);else if(i>f){b&&(j+=k);var l=d-g;c.scrollTop(j-l)}},_updateDialCode:function(b){var c,d=this.telInput.val(),e=this._getDialCode(d);if(e.length>1)c=d.replace(e,b);else{var f=d&&"+"!=d.substr(0,1)?a.trim(d):"";c=b+f}this._updateVal(c,!0)},_getDialCode:function(b){var c="";if("+"==b.charAt(0))for(var d="",e=0;eb?1:aa.length?!1:O(pa,a)}function ua(a){return O(na,a)?P(a,ia):P(a,K)}function va(a){var b=ua(a.toString());a.clear();a.append(b)}function P(a,b){for(var c=new B,d,f=a.length,e=0;e=e&&e<=f;++e)if(d=parseInt(c.substring(0,e),10),d in H)return b.append(c.substring(e)),d;return 0} -function Fa(a,b,c,d,f){if(0==a.length)return 0;a=new B(a);var e;null!=b&&(e=t(b,11));null==e&&(e="NonMatch");var g=a.toString();if(0==g.length)e=20;else if(L.test(g))g=g.replace(L,""),a.clear(),a.append(ua(g)),e=1;else{g=new RegExp(e);va(a);e=a.toString();if(0==e.search(g)){var g=e.match(g)[0].length,h=e.substring(g).match(M);h&&null!=h[1]&&0=a.e.length)throw"Phone number too short after IDD"; -c=Ea(a,c);if(0!=c)return f.w(c),c;throw"Invalid country calling code";}if(null!=b&&(e=b.m(),g=""+e,h=a.toString(),0==h.lastIndexOf(g,0))){var l=new B(h.substring(g.length)),h=t(b,1),g=new RegExp(u(h,2));Ga(l,b,null);b=l.toString();h=u(h,3);if(!O(g,a.toString())&&O(g,b)||3==Da(h,a.toString()))return c.append(b),d&&p(f,6,10),f.w(e),e}f.w(0);return 0} -function Ga(a,b,c){var d=a.toString(),f=d.length,e=t(b,15);if(0!=f&&null!=e&&0!=e.length&&(e=new RegExp("^(?:"+e+")"),f=e.exec(d))){var g=RegExp,h;h=t(b,1);h=u(h,2);g=new g(h);h=O(g,d);var l=f.length-1;b=t(b,16);if(null==b||0==b.length||null==f[l]||0==f[l].length){if(!h||O(g,d.substring(f[0].length)))null!=c&&0b.e.length)throw"The string supplied is too short to be a phone number";null!=g&&(a=new B,c=new B(b.toString()),Ga(c,g,a),f=c.toString(),g=t(g,1),g=u(g,3),2!=Da(g,f)&&(b=c,d&&p(e,7,a.toString())));d=b.toString();a=d.length;if(2>a)throw"The string supplied is too short to be a phone number";if(17a.length?!1:N(xa,a)}function Ca(a){return N(va,a)?O(a,pa):O(a,L)}function Da(a){var b=Ca(a.toString());a.clear();a.append(b)}function O(a,b){for(var c=new D,d,f=a.length,e=0;e=e&&e<=f;++e)if(d=parseInt(c.substring(0,e),10),d in J)return b.append(c.substring(e)),d;return 0} +function Na(a,b,c,d,f){if(0==a.length)return 0;a=new D(a);var e;null!=b&&(e=t(b,11));null==e&&(e="NonMatch");var g=a.toString();if(0==g.length)e=20;else if(M.test(g))g=g.replace(M,""),a.clear(),a.append(Ca(g)),e=1;else{g=new RegExp(e);Da(a);e=a.toString();if(0==e.search(g)){var g=e.match(g)[0].length,h=e.substring(g).match(ra);h&&null!=h[1]&&0=a.d.length)throw"Phone number too short after IDD"; +c=Ma(a,c);if(0!=c)return f.$(c),c;throw"Invalid country calling code";}if(null!=b&&(e=b.o(),g=""+e,h=a.toString(),0==h.lastIndexOf(g,0))){var l=new D(h.substring(g.length)),h=t(b,1),g=new RegExp(w(h,2));Oa(l,b,null);b=l.toString();h=w(h,3);if(!N(g,a.toString())&&N(g,b)||3==La(h,a.toString()))return c.append(b),d&&u(f,6,10),f.$(e),e}f.$(0);return 0} +function Oa(a,b,c){var d=a.toString(),f=d.length,e=t(b,15);if(0!=f&&null!=e&&0!=e.length&&(e=new RegExp("^(?:"+e+")"),f=e.exec(d))){var g=RegExp,h;h=t(b,1);h=w(h,2);g=new g(h);h=N(g,d);var l=f.length-1;b=t(b,16);if(null==b||0==b.length||null==f[l]||0==f[l].length){if(!h||N(g,d.substring(f[0].length)))null!=c&&0b.d.length)throw"The string supplied is too short to be a phone number";null!=g&&(a=new D,c=new D(b.toString()),Oa(c,g,a),f=c.toString(),g=t(g,1),g=w(g,3),2!=La(g,f)&&(b=c,d&&u(e,7,a.toString())));d=b.toString();a=d.length;if(2>a)throw"The string supplied is too short to be a phone number";if(17=Ba}}return N}catch(ab){return!1}});k("intlTelInputUtils.getExampleNumber",function(a){var b=J.ia();a=b.sa(a);return b.format(a,1)});})(); +function Qa(a){this.qa="\u2008";this.ga=new RegExp(this.qa);this.ha="";this.p=new D;this.aa="";this.j=new D;this.s=new D;this.k=!0;this.v=this.r=this.la=!1;this.pa=K.ka();this.w=0;this.c=new D;this.fa=!1;this.m="";this.b=new D;this.f=[];this.ia=a;this.xa=this.g=W(this,this.ia)}var Ra=new G;u(Ra,11,"NA"); +var Sa=/\[([^\[\]])*\]/g,Ta=/\d(?=[^,}][^,}])/g,Ua=RegExp("^[-x\u2010-\u2015\u2212\u30fc\uff0d-\uff0f \u00a0\u00ad\u200b\u2060\u3000()\uff08\uff09\uff3b\uff3d.\\[\\]/~\u2053\u223c\uff5e]*(\\$\\d[-x\u2010-\u2015\u2212\u30fc\uff0d-\uff0f \u00a0\u00ad\u200b\u2060\u3000()\uff08\uff09\uff3b\uff3d.\\[\\]/~\u2053\u223c\uff5e]*)+$"),Va=/[- ]/;function W(a,b){var c=Q(b)?Ka(a.pa,b):0,c=U(a.pa,T(c));return null!=c?c:Ra} +function Wa(a){for(var b=a.f.length,c=0;c=Ha}}return P}catch(ib){return!1}});m("intlTelInputUtils.getExampleNumber",function(a){var b=K.ka();a=b.ta(a);return b.format(a,1)});})(); diff --git a/lib/libphonenumber/src/utils.js b/lib/libphonenumber/src/utils.js index 1aefae4eb..1381133ff 100644 --- a/lib/libphonenumber/src/utils.js +++ b/lib/libphonenumber/src/utils.js @@ -28,14 +28,13 @@ function isValidNumber(number, countryCode) { } } -function formatNumber(val, addSuffix) { +function formatNumber(val, countryCode, addSuffix) { try { - // ignore numbers if they dont start with an intl dial code - if (val.substr(0, 1) != "+") { - return val; + var clean = val.replace(/\D/g, ""); + if (val.substr(0, 1) == "+") { + clean = "+" + clean; } - var clean = "+" + val.replace(/\D/g, ""); - var formatter = new i18n.phonenumbers.AsYouTypeFormatter(""); + var formatter = new i18n.phonenumbers.AsYouTypeFormatter(countryCode); var result; for (var i = 0; i < clean.length; i++) { result = formatter.inputDigit(clean.charAt(i)); @@ -47,8 +46,12 @@ function formatNumber(val, addSuffix) { if (addSuffix) { // hack to get formatting suffix var test = formatter.inputDigit('5'); - // if the number still contains formatting (there's always a space after the dial code) then we're good to go (else the number is too long and libphonenumber removed all the formatting) - if (test.indexOf(' ') !== -1) { + // again the "+44 " problem... (also affects "+45" apparently) + if (test.charAt(test.length - 1) == " ") { + test = test.substr(0, test.length - 1); + } + // if adding a '5' introduces a formatting char (i.e. it's length changes by >1) + if (test.length - result.length > 1) { // update the result to the new value (minus that last '5' we just added) result = test.substr(0, test.length - 1); } diff --git a/src/js/intlTelInput.js b/src/js/intlTelInput.js index 0db650f00..d620fa5f8 100644 --- a/src/js/intlTelInput.js +++ b/src/js/intlTelInput.js @@ -68,7 +68,7 @@ init: function() { // if in nationalMode, disable options relating to dial codes if (this.options.nationalMode) { - this.options.autoFormat = this.options.autoHideDialCode = false; + this.options.autoHideDialCode = false; } // chrome on android has issues with key events // backspace issues for inputs with type=text: https://code.google.com/p/chromium/issues/detail?id=184812 @@ -322,14 +322,16 @@ var addSuffix = !(e.which == keys.BSPACE && cursorAtEnd); that._handleInputKey(null, addSuffix); } - // prevent deleting the plus - var val = that.telInput.val(); - if (val.substr(0, 1) != "+") { - // newCursorPos is current pos + 1 to account for the plus we are about to add - var newCursorPos = (that.isGoodBrowser) ? input.selectionStart + 1 : 0; - that.telInput.val("+" + val); - if (that.isGoodBrowser) { - input.setSelectionRange(newCursorPos, newCursorPos); + // prevent deleting the plus (if not in nationalMode) + if (!that.options.nationalMode) { + var val = that.telInput.val(); + if (val.substr(0, 1) != "+") { + // newCursorPos is current pos + 1 to account for the plus we are about to add + var newCursorPos = (that.isGoodBrowser) ? input.selectionStart + 1 : 0; + that.telInput.val("+" + val); + if (that.isGoodBrowser) { + input.setSelectionRange(newCursorPos, newCursorPos); + } } } } else { @@ -638,11 +640,7 @@ var formatted; if (this.options.autoFormat && window.intlTelInputUtils) { - // don't try to add the suffix if we dont have a full dial code - if (!this._getDialCode(val)) { - addSuffix = false; - } - formatted = intlTelInputUtils.formatNumber(val, addSuffix); + formatted = intlTelInputUtils.formatNumber(val, this.selectedCountryData.iso2, addSuffix); } else { // no autoFormat, so just insert the original value formatted = val;