How to add an email mask inputmask in an input field inside a table?

0

How to add an email mask inputmask in a input field within a dynamically created %% table? Example:

<tr role="row" data-row-index="0">
    <td class="text-center">
        <input id="chkSelecionado" type="checkbox" />
    </td>
    <td class="form-group">@Html.TextBox("Contatos[0].Nome", "", new { @class = "form-control required" })</td>
    <td class="form-group">@Html.TextBox("Contatos[0].Telefone", "", new { @class = "form-control required telefone" })</td>
    <td class="form-group">@Html.TextBox("Contatos[0].Celular1", "", new { @class = "form-control celular" })</td>
    <td class="form-group">@Html.TextBox("Contatos[0].Email", "", new { @class = "form-control required" })</td>
</tr>

$(document).ready(function(){( )};
    
asked by anonymous 19.09.2017 / 18:57

1 answer

0

I've seen it solved, but I'll give you an answer if you need to get more than one mask, without having to call the method again:

HTML example:

<input type='text' class='mask' data-mask='cpf' name='cpf'>
<input type='text' class='mask' data-mask='telefone' name='telefone'>
<input type='text' class='mask' data-mask='email' name='email'>

Focusing on the function that adds the mask:

$(document).on('focusin','.mask',function(){
    var $this = $(this);
    var mask = $this.data('mask');
    makeMask(mask, $this);
});

Function that arrow the mask:

var makeMask = function (mask, $this) {
    if (mask === 'cpf') {
        $this.inputmask("999.999.999-99");
    }
    if (mask === 'telefone') {
        $this.inputmask({"mask": "+55 (99) [9] 9999-9999"});
    }
    if (mask === 'mail') {
        $this.inputmask("email");
    }
}

Advantages of this:

Whenever you need a mask anywhere, simply add the classname 'mask', and a data-attr (data-mask) with some mask that you have defined in your function.

    
19.09.2017 / 19:50