0
I’m using javascript to validate some form fields like phone and CPF. The script works correctly the way I want it, the problem is that when the user does not type all the numbers in the script so the script does not clean the input, it accepts that amount of numbers.
Take the test
function inputHandler(masks, max, event) {
var c = event.target;
var v = c.value.replace(/\D/g, '');
var m = c.value.length > max ? 1 : 0;
VMasker(c).unMask();
VMasker(c).maskPattern(masks[m]);
c.value = VMasker.toPattern(v, masks[m]);
}
var telMask = ['(99) 9999-99999', '(99) 99999-9999'];
var tel = document.querySelector('input[attrname=telephone1]');
VMasker(tel).maskPattern(telMask[0]);
tel.addEventListener('input', inputHandler.bind(undefined, telMask, 14), false);
var docMask = ['999.999.999-999', '99.999.999/9999-99'];
var doc = document.querySelector('#doc');
VMasker(doc).maskPattern(docMask[0]);
doc.addEventListener('input', inputHandler.bind(undefined, docMask, 14), false);
div {
font-family: Arial;
padding: 5px;
margin-bottom: 10px;
}
input {
box-sizing: border-box;
padding: 10px;
border-radius: 5px;
border-color: black;
width: 250px;
font-size: 1.3em;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-masker/1.1.0/vanilla-masker.min.js"></script>
<!-- Sandro Alvares -->
<div>
<label for="tel">Telefone</label>
<input attrname="telephone1" type="text">
<br><div>
<label for="doc">CPF/CNPJ</label>
<input id="doc" type="text">
I wanted help to make sure that if the user didn’t type the correct number, when he took the focus the input erased the numbers typed.
I believe the CPF mask is wrong! Or is it just me?
– NoobSaibot