Mascaras com jQuery e JS, trocando de campo no maxLength

No Comments »

Cara, se você tiver disposto à trocar de máscara, essas com ER são muito boas, e não te darão esse tipo de problema:
http://forum.imaster…ost__p__1532871

  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. /* Máscaras ER */
  5. function mascara(o,f){
  6.     v_obj=o
  7.     v_fun=f
  8.     setTimeout("execmascara()",1)
  9. }
  10. function execmascara(){
  11.     v_obj.value=v_fun(v_obj.value)
  12. }
  13. function mcep(v){
  14.     v=v.replace(/\D/g,"")                    //Remove tudo o que não é dígito
  15.     v=v.replace(/^(\d{5})(\d)/,"$1-$2")         //Esse é tão fácil que não merece explicações
  16.     return v
  17. }
  18. function mdata(v){
  19.     v=v.replace(/\D/g,"");                    //Remove tudo o que não é dígito
  20.     v=v.replace(/(\d{2})(\d)/,"$1/$2");      
  21.     v=v.replace(/(\d{2})(\d)/,"$1/$2");      
  22.  
  23.     v=v.replace(/(\d{2})(\d{2})$/,"$1$2");
  24.     return v;
  25. }
  26. function mrg(v){
  27.         v=v.replace(/\D/g,);
  28.         v=v.replace(/^(\d{2})(\d)/g,"$1.$2");
  29.         v=v.replace(/(\d{3})(\d)/g,"$1.$2");
  30.         v=v.replace(/(\d{3})(\d)/g,"$1-$2");
  31.         return v;
  32. }
  33. function mvalor(v){
  34.     v=v.replace(/\D/g,"");//Remove tudo o que não é dígito
  35.     v=v.replace(/(\d)(\d{8})$/,"$1.$2");//coloca o ponto dos milhões
  36.     v=v.replace(/(\d)(\d{5})$/,"$1.$2");//coloca o ponto dos milhares
  37.  
  38.     v=v.replace(/(\d)(\d{2})$/,"$1,$2");//coloca a virgula antes dos 2 últimos dígitos
  39.     return v;
  40. }
  41. function id( el ){
  42.         return document.getElementById( el );
  43. }
  44. function next( el, next )
  45. {
  46.         if( el.value.length >= el.maxLength )
  47.                 id( next ).focus();
  48. }
  49. </script>
  50. </head>
  51. <body>
  52.         Real: <input type="text" name="valor" onkeypress="mascara( this, mvalor ); next( this, ‘cep’ );" maxlength="14" />
  53.         <br />
  54.         CEP: <input type="text" name="cep" id="cep" onkeypress="mascara(this, mcep); next( this, ‘rg’ );" size="10" maxlength="9" value="" />
  55.         <br />
  56.         RG: <input type="text" name="rg" id="rg" onkeypress="mascara(this, mrg); next( this, ‘data’ );" size="14" maxlength="12" value="" />
  57.         <br />
  58.         Data: <input type="text" name="data" id="data" onkeypress="mascara(this, mdata);" size="14" maxlength="10" value="" />
  59. </body>
  60. </html>[/code]

Mas qnto ao que você pediu, é possível fazer com ER, e assim continuar usando a máscara jQuery. Porém dá um pouquinho de trabalho.. veja:[code]

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jquery.maskedinput.js"></script>
<script type="text/javascript">
$(document).ready(function(){
        var cep = $("input[name='cep']");
        var rg = $("input[name='rg']");
        var data = $("input[name='data']");

        $( cep ).mask('99999-999');
        $( rg ).mask('99.999.999-9');
        $( data ).mask('99/99/9999');

        $("input[name='cep']").keyup(function(){
                next( $(this), rg );
        });
        $("input[name='rg']").keyup(function(){
                next( $(this), data );
        });
});
function next( el, next )
{
        var val = $( el ).val();
        var filtrado = val.replace(/[^a-zA-Z0-9]/g, '');// ER para remover tudo oque não for nem letra e nem número    
        var mask = val.match(/[-.]/g);// ER para saber quantos caracteres da máscara fazem parte da string final

        if( filtrado.length == $( el ).attr('maxLength')-mask.length )
                $( next ).focus();
}
</script>
</head>
<body>
        CEP: <input type="text" name="cep" size="10" maxlength="9" value="" />
        <br />
        RG: <input type="text" name="rg" size="14" maxlength="12" value="" />
        <br />
        Data: <input type="text" name="data" size="14" maxlength="10" value="" />
</body>
</html>
maio 6th 2010 Não Classificados