Mascara telefones

This commit is contained in:
jp-lima 2025-09-02 10:14:58 -03:00
parent fb9d783f33
commit 5b63fa26ef

View File

@ -4,18 +4,32 @@ import InputMask from "react-input-mask";
function PatientForm({ onSave, onCancel }) {
const FormatTelefones = () => {}
const FormatTelefones = (valor) => {
const digits = String(valor).replace(/\D/g, '').slice(0, 11);
return digits
.replace(/(\d)/, '($1') // 123 -> 123.
.replace(/(\d{2})(\d)/, '$1) $2' )
.replace(/(\d)(\d{4})/, '$1 $2')
.replace(/(\d{4})(\d{4})/, '$1-$2')
}
const FormatCPF = (valor) => {
const digits = String(valor).replace(/\D/g, '').slice(0, 11);
return digits
.replace(/(\d{3})(\d)/, '$1.$2') // 123 -> 123.
.replace(/(\d{3})(\d)/, '$1.$2') // 123.456 -> 123.456.
.replace(/(\d{3})(\d{1,2})$/, '$1-$2'); // 123.456.789 -> 123.456.789-01
}
@ -57,19 +71,30 @@ function PatientForm({ onSave, onCancel }) {
[name]: value
});
console.log(name)
if(e.target.name.include('cpf')){
if(name.includes('cpf')){
let x = FormatCPF(e.target.value)
let cpfFormatado = FormatCPF(e.target.value)
console.log(x)
setFormData({...formData,
[name]: x,}
)
[name]: cpfFormatado,}
)}
else if(name.includes('telefone')){
let telefoneFormatado = FormatTelefones(value)
console.log(telefoneFormatado)
setFormData({...formData,
[name]: telefoneFormatado
})
}
};
};
// Função para buscar endereço pelo CEP