0

I am using intl-tel-input angular 4 for validate phone number ,when I added this its showing country code with phone number but if enter wrong phone its not showing error message. For this I am using this code :-

<ngx-intl-tel-input   name="phone" id ="phone" [(value)]="model.phone"  required="required" [preferredCountries]="preferredCountries" ></ngx-intl-tel-input>

this is my html file code and here my component.ts file code :-

  $("#phone").intlTelInput({
     utilsScript: "../../build/js/utils.js"
  });

when I am added this it showing error message:-

 "core.es5.js:1084 ERROR Error: Uncaught (in promise): TypeError: $(...).intlTelInput is not a function"

for this I am tried this code but no luck :-

  https://jsbin.com/yacokiyece/edit?html,css,js,output

this is git code which I follow :- https://github.com/webcat12345/ngx-intl-tel-input

Mangita
  • 595
  • 6
  • 24

1 Answers1

0

You'll need to add the code below in your default-country-ip.js file.

$("#phone").intlTelInput({
  initialCountry: "auto",
  geoIpLookup: function(callback) {
    $.get('//ipinfo.io', function() {}, "jsonp").always(function(resp) {
      var countryCode = (resp && resp.country) ? resp.country : "";
      callback(countryCode);
    });
  },
  utilsScript: "js/utils.js" // just for formatting/placeholders etc
});


  var telInput = $("#phone"),
  errorMsg = $("#error-msg"),
  validMsg = $("#valid-msg");

// initialise plugin
telInput.intlTelInput({
  utilsScript: "js/utils.js"
});

var reset = function() {
  telInput.removeClass("error");
  errorMsg.addClass("hide");
  validMsg.addClass("hide");
};

// on blur: validate
telInput.blur(function() {
  reset();
  if ($.trim(telInput.val())) {
    if (telInput.intlTelInput("isValidNumber")) {
      validMsg.removeClass("hide");
    } else {
      telInput.addClass("error");
      errorMsg.removeClass("hide");
    }
  }
});

// on keyup / change flag: reset
telInput.on("keyup change", reset);

Check out this repo for a much detailed guide

SamWanekeya
  • 546
  • 5
  • 10