Format phone numbers and CNPJ

Asked

Viewed 1,773 times

1

I’m racking my brain on how to display phone numbers and CNPJ formatted via PHP.

My ideas are either to create a script that when filling the dots and strokes are automatically placed (style when you fill the CPF only with numbers and the site scores automatically), which language I should use for this?

Or else the exit I think is easier, in charge echo delimit the score by the length of the string, someone would tell me which commands I should use (can be only a north, tell me which command does this I go after more information)?

  • 1

    You want a mask when typing or just to display?

  • I want to record this data in a local database, to later do a data collection and form a table.

  • 1

    Will that helps?

2 answers

1

Create a Function in jQuery to validate the CPF and/or CNPJ. If it is not something just visual, will have to do the treatment to save in the database, giving the .replace points and bar.

Example:

$(function () {
        $("[id$=txtCpfCnpj]").focusout(function () {
            $(this).unmask();
            $(this).val($(this).val().replace(/\D/g, ""));
        }).click(function () {
            $(this).val($(this).val().replace(/\D/g, "")).unmask();
        }).blur(function () {
            if ($(this).val().length == 11) {
                $(this).mask("999.999.999-99");
            } else if ($(this).val().length == 14) {
                $(this).mask("99.999.999/9999-99");
            }
        });
    });
  • Then I’ll search it! Thanks for the information! to try to make it work as a whole first, last I’ll run after :)

  • I was going to say that jQuery to validate CPF and CNPJ is exaggeration, but I saw the application of mask in the field, so ok.

-1


You can do it in several ways, I’ll show you the simplest one. There is a library of your own that you can "Import" through link .

<script type="text/javascript" src="js/jquery.mask.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>

add this script below in your HTML as well

 <script type="text/javascript">
    $(document).ready(function(){
      $('#cpf').mask('00000-000');
      $('#cnpj').mask('00.000.000/0000-00');
      $('#telefone').mask('0000-0000');
    });
 </script> 

And in your input just put the ID referring to what you entered in Javascript. But, you can also create a class and reference by $('.cpf').mask('00000-000); however, you will have to change your input and instead of id put class='cpf'

<input type='text' name='cpf' id='cpf'>

<input type='text' name='cnpj' id='cnpj'>

<input type='text' name='telefone' id='telefone'>

Remembering that if you want to send directly to the database, in its variables you will have to do a process to eliminate the points, I will leave below a simple example of how it should be done.

$chars = array(".","/","-", "*"); /* aqui indico os caracteres que desejo remover */

$cpf = str_replace($chars, "", $_POST['cpf']); /* através de str_replace insiro somente os números invés de caracteres */
$cnpj = str_replace($chars, "", $_POST['cnpj']);
$telefone = str_replace($chars, "", $_POST['telefone']);
  • Did it @Maicomrodeghiero ?

  • I’m trying to apply here, I’m well Noob still, to just in the first semester of Systems Analysis and Development. But I’m trying to develop this stop on the outside for my work here. :)

  • just follow the step by step I told you, copy and paste these first links, with the <script> tag and then just do as I indicated, it will work

  • The part of removing the special characters ta working perfectly! thank you very much! But and for me to pull them from the DB and display formatted?

  • you already have a function to pull this data from the database through select ? if yes put the same tag after the fields but instead of 0 fill with 9

  • I found two problems, I don’t know if it’s my Noob question or if you really have problems: 1º - $('.Cpf'). Mask('00000-000); /* missing a quote at the end 2º - $chars = array(".","/","-", "", ""); / the backslash dropped the end of the array.

  • the correct is to put the quotes and leave it => $('.Cpf') as I did in the above answer ... and remove the backslash

  • I solved the display with this code: echo '('.substr($tel_contato, 0, 2).') '.substr($tel_contato, 2, 4).'-'.substr($tel_contato,6); Thanks for the help!

  • if you have any other difficulty just comment here

  • All settled! Thank you very much for the willing time and attention!

Show 5 more comments

Browser other questions tagged

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