Recover Cpf with database mask

Asked

Viewed 136 times

0

I have a Cpf record in the database where it is saved without the mask, when displaying on the screen for the user I need the number to be format for the Cpf type, how to do?

I managed to put the mask when the user is typing using jquery Mask like this:

$(document).ready(function () {
    var $Cpf = $("#Cpf");
    $Cpf.mask('000.000.000-00', { reverse: true });
});

however need to be placed tb at the time of data display for the user.

1 answer

0

follow as ta in the documentation: put this in your 'head'

  <script src='https://cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js'></script>

<script src='https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.10/jquery.mask.js'></script>

in your input will stay like this:

<input id="cpf" name="cpf" type="text" class="cpf" value="{variavel cpf}">

and at the end put this

    <script>
    $(document).ready(function(){
    $('.date').mask('00/00/0000');
    $('.time').mask('00:00:00');
    $('.date_time').mask('00/00/0000 00:00:00');
    $('.cep').mask('00000-000');
    $('.phone').mask('(91) 00000-0000');
    $('.phone_with_ddd').mask('(00) 0000-0000');
    $('.phone_us').mask('(000) 000-0000');
    $('.mixed').mask('AAA 000-S0S');
    $('.cpf').mask('000.000.000-00', {reverse: true});
    $('.cnpj').mask('00.000.000/0000-00', {reverse: true});
    $('.money').mask('000.000.000.000.000,00', {reverse: true});
    $('.money2').mask("#.##0,00", {reverse: true});
    $('.ip_address').mask('0ZZ.0ZZ.0ZZ.0ZZ', {
        translation: {
        'Z': {
            pattern: /[0-9]/, optional: true
        }
        }
    });
    $('.ip_address').mask('099.099.099.099');
    $('.percent').mask('##0,00%', {reverse: true});
    $('.clear-if-not-match').mask("00/00/0000", {clearIfNotMatch: true});
    $('.placeholder').mask("00/00/0000", {placeholder: "__/__/____"});
    $('.fallback').mask("00r00r0000", {
        translation: {
            'r': {
            pattern: /[\/]/,
            fallback: '/'
            },
            placeholder: "__/__/____"
        }
        });
    $('.selectonfocus').mask("00/00/0000", {selectOnFocus: true});
    });

</script>

or try this:

 $string = "33322255588";
    $string1 = substr("$string", 0, 3);
    $string2 = substr("$string", 3, 3);
    $string3 = substr("$string", 6, 3);
    $string4 = substr("$string", 9, 10);
    echo $string1.'.'.$string2.'.'.$string3.'-'.$string4;

to save in the bank you have to take the formatting then you can do it:

    $string = "333.222.555-88";
    $result = explode('.', $string );
    $result2 = explode('-', $result[2] );
    $result3 = $result[0].$result[1].$result2[0].$result2[1];
    echo $result3;
  • It did not work, the mask is not yet applied, it is coming from the bank without mask and so remains

  • ta ta la vc ta displaying this within an input ?

  • that way: <input required="required" type="text" class="form-control" id="Cpf" name="Cpf" placeholder="Ex.: 010.011.111-00" maxlength="14">

  • when you type inside this input it formats?

  • Yeah, it just doesn’t work when it comes from the bank...

  • ta la so convert the values, vc ta using php in the back?

  • I edited my answer, look at it at the end of my answer there’s one thing I did in php, I don’t know which language in the back q vc are using, but basically I formatted the string using php by taking each possible and putting '-' and '.' because Cpf’s string number value never changes, this way this is a solution, just remember that you have to treat this value when saving again in the bank

  • I’m using c#, I don’t want to have to hand it to you but I think it’s gonna be the way

  • yes vc will have to treat it p display p user(03/02/1992) and treat when inserting in the bank(1992-02-03)... in these formats

Show 4 more comments

Browser other questions tagged

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