我正在使用国际电话输入角度4来验证电话号码,当我将其显示的国家代码与电话号码一起添加时,但是如果输入错误的电话则不会显示错误消息。为此,我正在使用此代码:-
<ngx-intl-tel-input name="phone" id ="phone" [(value)]="model.phone" required="required" [preferredCountries]="preferredCountries" ></ngx-intl-tel-input>
这是我的html文件代码,这里是我的component.ts文件代码:-
$("#phone").intlTelInput({
utilsScript: "../../build/js/utils.js"
});
当我添加此消息时,它显示错误消息:-
"core.es5.js:1084 ERROR Error: Uncaught (in promise): TypeError: $(...).intlTelInput is not a function"
为此,我尝试了此代码,但没有运气:-
https://jsbin.com/yacokiyece/edit?html,css,js,output
这是我遵循的git代码:- https://github.com/webcat12345/ngx-intl-tel-input
答案 0 :(得分:0)
您需要在default-country-ip.js文件中添加以下代码。
$("#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);
查看此repo以获得更详细的指南