Маска телефона
document.addEventListener('DOMContentLoaded', function () {
document.querySelectorAll('input[type="tel"]').forEach(itel => {
if(!itel.classList.contains('masked')){
for(let ev of ['input', 'blur', 'focus']){
itel.addEventListener(ev, (e) => {
clearVal = e.target.dataset.phoneClear,
pattern = e.target.dataset.phonePattern,
matrix_def = '+7(___) ___-__-__',
matrix = pattern ? pattern : matrix_def,
i = 0,
def = matrix.replace(/\D/g, ''),
val = e.target.value.replace(/\D/g, '');
if (clearVal !== 'false' && e.type === 'blur') {
if (val.length < matrix.match(/([\_\d])/g).length) {
e.target.value = '';
return;
}
}
if (def.length >= val.length) val = def;
e.target.value = matrix.replace(/./g, function (a) {
return /[_\d]/.test(a) && i < val.length ? val.charAt(i++) : i >= val.length ? '' : a
});
});
}
itel.classList.add('masked');
}
});
});