最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery International Telephone number input - Stack Overflow

programmeradmin4浏览0评论

I am trying to follow jquery tutorial on .html to create input type number with country code in a form. However, in my case though flag is appearing to change, the country code is not added inside the input field as they show in the demo.

Following is the code they have provided:

<head>
<meta charset="utf-8">
<title>International Telephone Input</title>
<link rel="stylesheet" href="build/css/intlTelInput.css">
<link rel="stylesheet" href="build/css/demo.css">
</head>
   <body>

<form>
  <input id="mobile-number" type="tel">
  <button type="submit">Submit</button>
</form>

<!-- Load jQuery from CDN so can run demo immediately -->
<script src=".11.1/jquery.min.js"></script>
<script src="build/js/intlTelInput.js"></script>
<script>
  $("#mobile-number").intlTelInput({
    //allowExtensions: true,
    //autoFormat: false,
    //autoHideDialCode: false,
    //autoPlaceholder: false,
    //defaultCountry: "auto",
    //ipinfoToken: "yolo",
    //nationalMode: false,
    //numberType: "MOBILE",
    //onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
    //preferredCountries: ['cn', 'jp'],
    //preventInvalidNumbers: true,
    utilsScript: "lib/libphonenumber/build/utils.js"
  });
</script>

Here is the link where I am trying to implement it: .html

I have tried changing the mented out options in script but it doesn't help.

I am trying to follow jquery tutorial on http://www.jqueryscript/form/jQuery-International-Telephone-Input-With-Flags-Dial-Codes.html to create input type number with country code in a form. However, in my case though flag is appearing to change, the country code is not added inside the input field as they show in the demo.

Following is the code they have provided:

<head>
<meta charset="utf-8">
<title>International Telephone Input</title>
<link rel="stylesheet" href="build/css/intlTelInput.css">
<link rel="stylesheet" href="build/css/demo.css">
</head>
   <body>

<form>
  <input id="mobile-number" type="tel">
  <button type="submit">Submit</button>
</form>

<!-- Load jQuery from CDN so can run demo immediately -->
<script src="http://ajax.googleapis./ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script src="build/js/intlTelInput.js"></script>
<script>
  $("#mobile-number").intlTelInput({
    //allowExtensions: true,
    //autoFormat: false,
    //autoHideDialCode: false,
    //autoPlaceholder: false,
    //defaultCountry: "auto",
    //ipinfoToken: "yolo",
    //nationalMode: false,
    //numberType: "MOBILE",
    //onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
    //preferredCountries: ['cn', 'jp'],
    //preventInvalidNumbers: true,
    utilsScript: "lib/libphonenumber/build/utils.js"
  });
</script>

Here is the link where I am trying to implement it: https://www.easyac./mobile/verifynumber.html

I have tried changing the mented out options in script but it doesn't help.

Share Improve this question asked Mar 5, 2015 at 16:34 Abhishek SinghAbhishek Singh 1971 gold badge4 silver badges18 bronze badges 0
Add a ment  | 

7 Answers 7

Reset to default 6

Ok I solved this problem. So basically here are the option fields that must be true and we need to place the below script before </head> tag.

Script:

<script>
$(function() {
$("#mobile-number").intlTelInput({
allowExtensions: true,
autoFormat: false,
autoHideDialCode: false,
autoPlaceholder: false,
defaultCountry: "auto",
ipinfoToken: "yolo",
nationalMode: false,
numberType: "MOBILE",
//onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
//preferredCountries: ['cn', 'jp'],
preventInvalidNumbers: true,
utilsScript: "lib/libphonenumber/build/utils.js"
});
});
</script>

Place it before closing head tag and remember to call $("#mobile-number").intlTelInput(); as it is important.

You just forget to put your jquery code inside de document.ready.

see below:

$(document).ready(function(){

    $("#mobile-number").intlTelInput({
        //allowExtensions: true,
        //autoFormat: false,
        //autoHideDialCode: false,
        //autoPlaceholder: false,
        //defaultCountry: "auto",
        //ipinfoToken: "yolo",
        //nationalMode: false,
        //numberType: "MOBILE",
        //onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
        //preferredCountries: ['cn', 'jp'],
        //preventInvalidNumbers: true,
        utilsScript: "lib/libphonenumber/build/utils.js"
    });
});

Country code will always be in input field with this code

$("#mobile-number").on("blur keyup change", function() {
if($(this).val() == '') {
    var getCode = $("#mobile-number").intlTelInput('getSelectedCountryData').dialCode;
    $(this).val('+'+getCode);
}});
$(document).on("click",".country",function(){
    if($("#phone").val() == '') {
        var getCode = $("#mobile-number").intlTelInput('getSelectedCountryData').dialCode;
        $("#mobile-number").val('+'+getCode);
}});

$(function() {
$("#mobile-number").intlTelInput({
   autoHideDialCode: false,
   autoPlaceholder: false,
   nationalMode: false
});
});

Why country flag and country code are not stable on page post back.

Here is what I have tried so far:

<script>
        $(document).ready(function () {
            $("#Phone").intlTelInput({
                allowDropdown: true,
                // autoHideDialCode: false,
                // autoPlaceholder: "off",
                // dropdownContainer: "body",
                // excludeCountries: ["us"],
                defaultCountry: "auto",
                // formatOnDisplay: false,
                //geoIpLookup: function (callback) {
                //    $.get("http://ipinfo.io", function () { }, "jsonp").always(function (resp) {
                //        var countryCode = (resp && resp.country) ? resp.country : "";
                //        callback(countryCode);
                //    });
                //},
                //initialCountry: "auto",
                // nationalMode: false,
                // onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
                // placeholderNumberType: "MOBILE",
                // preferredCountries: ['in','pk', 'np','bd', 'us','bt','sg','lk','ny','jp','hk','cn'],
                // separateDialCode: true,
                utilsScript: "build/js/utils.js"
            });

            $("#Phone").on("countrychange", function (e, countryData) {
                $("#hdnPhone").val(countryData.dialCode);
            });
        });
  </script>
<script>
        $(document).ready(function () {
            $("#Phone").val('');
            var HdnVal = $("#hdnPhone").val();

            if (HdnVal != '') {
                var countryData = $("#Phone").intlTelInput("getSelectedCountryData");

                $("#hdnPhone").val(countryData.dialCode);
            }
        });

    </script>
try this code

$("#mobile-number").intlTelInput({
       // allowExtensions: true,
       //autoFormat: false,
        autoHideDialCode: false,
       // autoPlaceholder: false,
       // defaultCountry: "auto",
        //ipinfoToken: "yolo",
        nationalMode: false,
       // numberType: "MOBILE",
       // onlyCountries: ['us', 'gb', 'ch', 'ca', 'do'],
        //preferredCountries: ['cn', 'jp'],
        //preventInvalidNumbers: true,
        utilsScript: "lib/libphonenumber/build/utils.js"
    });
});
 //if you want to get coutry code value in input box then set  false 
                   nationalMode: false,

 separateDialCode: false,

Pease set this two lines in your function property it will run

发布评论

评论列表(0)

  1. 暂无评论