YouTube video: https://www.youtube.com/watch?v=r-8isv_TnVA
-
-
Save raloliver/f5ad353db4e7e59d79a45fb37d8ae360 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const masks = { | |
cpf (value) { | |
return value | |
.replace(/\D+/g, '') | |
.replace(/(\d{3})(\d)/, '$1.$2') | |
.replace(/(\d{3})(\d)/, '$1.$2') | |
.replace(/(\d{3})(\d{1,2})/, '$1-$2') | |
.replace(/(-\d{2})\d+?$/, '$1') | |
}, | |
cnpj (value) { | |
return value | |
.replace(/\D+/g, '') | |
.replace(/(\d{2})(\d)/, '$1.$2') | |
.replace(/(\d{3})(\d)/, '$1.$2') | |
.replace(/(\d{3})(\d)/, '$1/$2') | |
.replace(/(\d{4})(\d)/, '$1-$2') | |
.replace(/(-\d{2})\d+?$/, '$1') | |
}, | |
phone (value) { | |
return value | |
.replace(/\D+/g, '') | |
.replace(/(\d{2})(\d)/, '($1) $2') | |
.replace(/(\d{4})(\d)/, '$1-$2') | |
.replace(/(\d{4})-(\d)(\d{4})/, '$1$2-$3') | |
.replace(/(-\d{4})\d+?$/, '$1') | |
}, | |
cep (value) { | |
return value | |
.replace(/\D+/g, '') | |
.replace(/(\d{5})(\d)/, '$1-$2') | |
.replace(/(-\d{3})\d+?$/, '$1') | |
}, | |
pis (value) { | |
return value | |
.replace(/\D+/g, '') | |
.replace(/(\d{3})(\d)/, '$1.$2') | |
.replace(/(\d{5})(\d)/, '$1.$2') | |
.replace(/(\d{5}\.)(\d{2})(\d)/, '$1$2-$3') | |
.replace(/(-\d)\d+?$/, '$1') | |
}, | |
money (value) { | |
const cleanValue = +value.replace(/\D+/g, '') | |
const options = { style: 'currency', currency: 'BRL' } | |
return new Intl.NumberFormat('pt-br', options).format(cleanValue / 100) | |
} | |
} | |
document.querySelectorAll('input').forEach($input => { | |
const field = $input.dataset.js | |
$input.addEventListener('input', e => { | |
e.target.value = masks[field](e.target.value) | |
}, false) | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<meta charset="utf-8"> | |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/milligram/1.4.1/milligram.min.css" integrity="sha512-xiunq9hpKsIcz42zt0o2vCo34xV0j6Ny8hgEylN3XBglZDtTZ2nwnqF/Z/TTCc18sGdvCjbFInNd++6q3J0N6g==" crossorigin="anonymous" /> | |
<title>Vanilla JS Masks</title> | |
<label>CPF: 000.000.000-00</label> | |
<input type="text" data-js="cpf"> | |
<label>CNPJ: 00.000.000/0000-00</label> | |
<input type="text" data-js="cnpj"> | |
<label>Telefone: (00) 00000-0000</label> | |
<input type="text" data-js="phone"> | |
<label>CEP: 00000-000</label> | |
<input type="text" data-js="cep"> | |
<label>PIS: 000.00000.00-0</label> | |
<input type="text" data-js="pis"> | |
<label>Moeda: R$ 0.000.000,00</label> | |
<input type="text" data-js="money"> | |
<script type="module" src="./app.js"></script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment