How to use Jquery.Mask for input CPF using []

Asked

Viewed 69 times

2

I am trying to use Jquery.Mask to insert a mask to a field with CPF but it does not work when the field has the following format:

$("#Cpfpessoa[]")

The input is like this:

<input type="text" id="CPFPessoa[]" name="CPFPessoa[]" size="15" class="form-control">
    $(document).ready(function () { 
        var $seuCampoCpf = $("#CPFPessoa[]");
        $seuCampoCpf.mask('000.000.000-00', {reverse: true});
    });

I need the field to have this format because I am using a form that can be added extra fields.

1 answer

4


You only wear clasps [] in the name, not in the id.

But don’t use id as you will be able to repeat the field (see this question about id repetition). You can use the attribute as a selector name:

var $seuCampoCpf = $("[name='CPFPessoa[]']");

Or change the id for class:

<input type="text" class="CPFPessoa" name="CPFPessoa[]" size="15" class="form-control">

And the selector:

var $seuCampoCpf = $(".CPFPessoa");
  • Thanks @Sam, thank you so much for the great tip.

Browser other questions tagged

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