You can mask a field using:
Jquery Mask Plugin
Documentation
Example of use:
$(document).ready(function(){
$('#telefone').mask('(00) 0000-0000');
});
Or in html itself:
<input type="text" name="telefone" data-mask="(00) 0000-0000" data-mask-selectonfocus="true" />
You can also apply callback on order... there are several examples in the documentation that fit your need..
For cases where the state has the ninth digit, you can use the
On-the-fly change: Example taken from the documentation, needs only
adaptation to their needs:
var options = {onKeyPress: function(cep, e, field, options){
var masks = ['00000-000', '0-00-00-00'];
mask = (cep.length>7) ? masks[1] : masks[0];
$('.crazy_cep').mask(mask, options);
}};
$('.crazy_cep').mask('00000-000', options);
Or the solution @Wallace provided in the comments that would look like this:
$(document).ready(function(){
$('#telefone').mask('(00) 0000-0000#');
});
Or the following expression:
$(document).ready(function(){
$('#telefone').mask('(00) 0000-00009');
});
Where 0 would be mandatory and 9 optional;
@Wallace also cited validation using HTML5.
From what I understand you want to mask what the user types is this?
– Rafael Withoeft
More or less, like... When he clicks on the input already appears the parentheses to put the ddd for example, has a default type for this?
– Everton Luis
@Everton In case you’re wanting to mask one
input
. I made an edit on your question by changing the title. You can reverse it if there are no improvements.– Renan Gomes