Skip to content
This repository has been archived by the owner on Apr 6, 2023. It is now read-only.

Commit

Permalink
feat: add js for phone input
Browse files Browse the repository at this point in the history
  • Loading branch information
GusBaamonde committed Apr 1, 2019
1 parent 932779d commit 2213f89
Showing 1 changed file with 57 additions and 0 deletions.
57 changes: 57 additions & 0 deletions assets/js/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,60 @@ $(".show-hide").click(function() {
$(this).removeClass("ms-icon icon-hide").addClass("ms-icon icon-view");
}
});

// Input phone number

var inputPhone = document.querySelector(".phone-number"),
errorMsg = document.querySelector(".error-phone-number");

var errorMap = [
"Número inválido",
"Código de area inválido",
"Muy corto",
"Muy largo",
"Número inválido"
];

// initialise plugin

var iti = window.intlTelInput(inputPhone, {
separateDialCode: true,
initialCountry: "auto",
hiddenInput: "full_phone",
geoIpLookup: function(callback) {
$.get('https://ipinfo.io', function() {}, "jsonp").always(function(resp) {
var countryCode = (resp && resp.country) ? resp.country : "ar";
callback(countryCode);
});
},
utilsScript: "../js/main.js"
});

var reset = function() {
inputPhone.classList.remove("error");
errorMsg.innerHTML = "";
errorMsg.classList.add("hide");
};

// on blur: validate
inputPhone.addEventListener('blur', function() {
reset();

if (inputPhone.value.trim()) {
if (iti.isValidNumber()) {
$(inputPhone).closest('.field-item').removeClass('error');
} else {

inputPhone.classList.add("error");
var errorCode = iti.getValidationError();
$(inputPhone).closest('.field-item').attr('data-error', errorMap[errorCode]);
$(inputPhone).closest('.field-item').find('.error-phone-number').text(errorMap[errorCode]);
$(inputPhone).closest('.field-item').addClass('error');
}
} else {
$(inputPhone).closest('.field-item').removeClass('error');
}
});

inputPhone.addEventListener('change', reset);
inputPhone.addEventListener('keyup', reset);

0 comments on commit 2213f89

Please sign in to comment.