formha/static/f_contact/re_phone.js

19 lines
714 B
JavaScript

import {simpleNotification} from '../z_comps/notify.js';
// simpleNotification("test", "sample", "error");
const phoneInput = document.getElementById("num_tel");
phoneInput.addEventListener("change", () => {
let raw = phoneInput.value.replace(/\D/g, ''); // quitar todo lo que no sea número
if (raw.length === 10) {
// aplicar formato XX XXXX XXXX
const formatted = `${raw.slice(0, 2)} ${raw.slice(2, 6)} ${raw.slice(6, 10)}`;
phoneInput.value = formatted;
} else {
// no hace nada si no son 10 dígitos, pero podrías notificar si quieres
simpleNotification("Error", "El número de teléfono debe tener 10 dígitos.", "error");
}
});