Change RG mask [duplicate]

1

The RG mask comes with a different pattern that I need!

RG Code:

function Rg(v){
        v=v.replace(/\D/g,"");                                     
        v=v.replace(/(\d)(\d{4})$/,"$1.$2");             
        v=v.replace(/(\d)(\d{4})$/,"$1.$2");   
        v=v.replace(/(\d)(\d)$/,"$1-$2");              
      return v
}

In this code it plays the RG with the default: 88888.888-8

And the standard I need is: 88.888.888-8

Thanks for the other answers. And I would like to add a question:

"Can you use a if at the time of typing to check how many numbers are informed to standardize on the function?"     

asked by anonymous 24.06.2014 / 15:50

2 answers

1

In pure JavaScript and without any validation, the function code should look like this:

function Rg(v){
    v=v.replace(/\D/g,"");
    v=v.replace(/(\d{2})(\d{3})(\d{3})(\d{1})$/,"$1.$2.$3-$4");
    return v;
}

I have prepared an example with error checking so you can see if that is what you need. The function only knows how to work with arguments of exactly 9 digits, that is, it works more or less like the code you already had.

link

Answering your second question: yes, you can check the user input, there is more than one way to do this.

In this example above, I have used if inside the Rg() function, and I communicate typing error through an Exception; another implementation would be pre-validation inside the callback function imprimeFormatado() , avoiding the call of the function Rg() (via if ) if the number of digits is incorrect. However, if the Rg() function is called in more than one place, I recommend implementing it as I did.

I hope it helps!

    
24.06.2014 / 21:12
0

You can use maskedinput-1.3.js (JQuery) look at the example below:
Note: you need to check the path of .js files and fix it if necessary. Below are examples of how to set masks for various input types.

<head>

<script src="jquery.js" type="text/javascript"></script>
<script src="maskedinput-1.3.js" type="text/javascript"></script>

    // ...

</head><br/>


<form><br/>
       Telefone: <input type="text" id="telefone" /<br/>
       CEP:      <input type="text" id="cep" /><br/>
       Data:     <input type="text" id="data" /><br/>
       CNPJ:     <input type="text" id="cnpj" /><br/>
       RG:       <input type="text" id="rg" /><br/>
       Agência:  <input type="text" id="agencia" /><br/>
       Comta:    <input type="text" id="conta" /><br/>
</form><br/>

<script type="text/javascript"><br/>

    jQuery(document).ready(function($) {<br/>

            $("#telefone").mask("(99) 9999-9999");     // Máscara para TELEFONE

            $("#cep").mask("99999-999");    // Máscara para CEP

            $("#data").mask("99/99/9999");    // Máscara para DATA

            $("#cnpj").mask("99.999.999/9999-99");    // Máscara para CNPJ

            $('#rg').mask('99.999.999-9');    // Máscara para RG<br/>

            $('#agencia').mask('9999-9');    // Máscara para AGÊNCIA BANCÁRIA

            $('#conta').mask('99.999-9');    // Máscara para CONTA BANCÁRIA

    }); <br/>

</script><br/>

library link to download: maskedinput-1.3.js

    
24.06.2014 / 16:25