Telephone Field Mask

Asked

Viewed 3,847 times

-1

How is it possible to make a mask in a phone input only to accept numbers and already enter the formatting (xx) xxxxx-xxxx

  • This can help you: https://igorescobar.github.io/jQuery-Mask-Plugin/

  • This question can be answered by viewing this other: https://answall.com/questions/282311/validates%C3%A7%C3%A3o-for-field-type-telephone? Rq=1

3 answers

3

I learned in this site same, to use of this occurrence, as follows:

$("#telefone, #celular").mask("(00) 0000-0000");
 <script 
 src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"> </script>
 <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.11/jquery.mask.min.js"></script>
 
 <label>Telefone</label>
<input type="text" class="form-control" placeholder="Telefone" id="telefone" name="telefone" ><br>
<label>Celular</label>
<input type="text" class="form-control" placeholder="Celular" id="celular" name="celular" ><br>

1

The example below contemplates cell phone with 9 digits (Cellular São Paulo)

var options = {
    onKeyPress: function (phone, e, field, options) {
        var masks = ['(00) 0000-00000', '(00) 00000-0000'];
        var mask = (phone.length > 14) ? masks[1] : masks[0];
        $('#celular').mask(mask, options);
    }
};

$('#celular').mask('(00) 0000-00000', options);

1

Using jQuery, you can do it this way:

$('#id_do_campo').mask('(99) 99999-9999');

Browser other questions tagged

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