我正在使用 intl-tel-input angular 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
您需要在默认的国家/地区 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);
查看此存储库以获取更详细的指南