quarta-feira, 26 de fevereiro de 2014

Máscara de Telefone com 9 dígitos em JavaScript


Este exemplo mostra como implementar uma função JavaScript para adicionar mais um dígito ao número de telefone.

 

<html>

<head>

    <title>Máscara de Telefone com 9 dígitos em JavaScript</title>

<script type="text/javascript">

/* Máscaras ER */

function mascaraTel(o, f) {

    v_obj = o;

    v_fun = f;

    setTimeout(execmascara, 1);

}

function execmascara() {

    v_obj.value = v_fun(v_obj.value)

}

function mtel(v) {

    v=v.replace(/\D/g,"");

    v=v.replace(/^(\d{2})(\d)/g,"($1) $2");

    v=v.replace(/(\d)(\d{4})$/,"$1-$2");

    return v;

}

function id(el) {

          return document.getElementById(el);

}

window.onload = function() {

          var $telefone = id('mascaraTel');

          $telefone.onkeyup = function() {

                    mascaraTel( this, mtel );

          }

          $telefone.onblur = function() {

                    var v = this.value;

                    if( v.indexOf('(11)') !== -1 && v.length === 14) {

                              this.value = v.replace(/\((\d{2})\) (\d{4})-(\d{4})/g,'($1) 9$2-$3');

                    }

          }

}

</script>

 

</head>

<body>

          <h1 style="color:#FF9933">Máscara de Telefone com 9 dígitos em JavaScript</h1>

    <label type="text" style="font-family:Verdana,Arial;" >Telefone</label>

    <input type="text" name="mascaraTel" id="mascaraTel" maxlength="15" />

 

</body>

</html>

Nenhum comentário:

Postar um comentário