CPF mask with pure Avascript

Asked

Viewed 716 times

0

I have the following mask code using javascript regular expressions

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title> simple document</title>

</head>

<body>
<center>
<form id="formulario">
    <fieldset>
        <legend>
            Validação de formulário simples da XTi feito com expressões regulares
        </legend>

        <label for="cpf">CPF:</label>
        <input name="cpf" type="text" maxlength="11" /><br />

        <input type="button" value="validar();" />
    </fieldset>
</form>
</center>
<script type="text/javascript">
        var formulario = document.getElementById("formulario");
        var cpf = formulario.cpf;
        var re_cpf = /^([\d]{3})([\d]{3})([\d]{3})([\d]{2})$/;

        cpf.onkeyup = function(){
            cpf.value = cpf.value.replace(re_cpf, "$1.$2.$3-$4");
        }
</script>
</body>
</html>

The problem is that the dots and the dash only appear after the person finishes typing, and not at the same time as she is typing, how could it solve this ?

  • 1

    See https://answall.com/q/278167/101

  • Change the line cpf.value = cpf.value.replace(re_cpf, "$1.$2.$3-$4"); for mascara(cpf); and can also delete the line var re_cpf = /^([\d]{3})([\d]{3})([\d]{3})([\d]{2})$/; that will no longer serve.

  • The question you put on the Link did not solve the problem, Please remove the duplicate tag...

  • @Demetrius explain why you haven’t solved.

  • http://jsfiddle.net/h31ykrfj/

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.