Change RG mask

Asked

Viewed 23,140 times

1

The RG mask comes with a different pattern in which 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 that code he plays the RG with the pattern: 88888.888-8

And the pattern I need is : 88.888.888-8

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

"You can use a if at the time of typing to check both numbers reported to standardize on the function?"

  • Hello, look at this link it shows a Masked input exactly the way you need it. Masked Input

  • Thanks for the info, but that’s not exactly what I need!

  • 1

    Dude, I don’t know why you want it in this format, but... The amount of RG numbers varies, has been having more and less numbers, depending on where the person is, it may not suit this amount of numbers you want to check.

  • It’s expensive ... The problem is that when you are "pawn" you must obey when you are passed a thing!

  • And this one: http://www.duocriativa.com.br/janela/blog/2012/06/10/application-mascaras-a-campos-de-formulas-com-plugin-jquery

  • I know, but in case, complete with zeros on the left, total can have up to 9 even.

  • Repeated question: http://answall.com/questions/22431/express%C3%A3o-regular-para-rg

Show 2 more comments

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’ve prepared an example with error checking for you to check if this is what you need. The function only knows how to work with arguments of exactly 9 digits, IE, works more or less like the code you already had.

http://jsfiddle.net/ruipimentel/667Bw/1/

To answer your second question: yes, it is possible to check the user input, including there is more than one way to do this.

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

I hope it helps!

0

You can use maskedinput-1.3. js(Jquery) look at the example below:
Note: it is necessary to check the path of the arches . js and correct it if necessary. Below examples of how to define 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 for download: maskedinput-1.3. js

  • It’s exactly the link I put in the comment

  • ah beauty, sorry aew! I use this tbm!

  • @Rogerscorrêa I edited your code but I’m not sure if it has unnecessary tags. Now that it is already in code format, could make the necessary adjustments?

Browser other questions tagged

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