Skip to content

Instantly share code, notes, and snippets.

@vinicius-stutz
Last active May 8, 2024 19:19
Show Gist options
  • Save vinicius-stutz/6caf02d4936069c69513 to your computer and use it in GitHub Desktop.
Save vinicius-stutz/6caf02d4936069c69513 to your computer and use it in GitHub Desktop.
Máscara p/ telefones com 8 ou 9 dígitos (jquery.mask.js)
<!DOCTYPE html>
<html>
<head>
<title>Máscaras de telefone (Brasil)</title>
<script type='text/javascript' src='//code.jquery.com/jquery-compat-git.js'></script>
<script type='text/javascript' src='//igorescobar.github.io/jQuery-Mask-Plugin/js/jquery.mask.min.js'></script>
</head>
<body>
<p>
<label>
Máscara para campo já com máscara<br />
<input type="text" class="phone" value="(21) 12345-6789" />
</label>
</p>
<p>
<label>
Máscara para campo SOMENTE números<br />
<input type="text" class="phone" value="21123456789" />
</label>
</p>
</body>
</html>
var behavior = function (val) {
return val.replace(/\D/g, '').length === 11 ? '(00) 00000-0000' : '(00) 0000-00009';
},
options = {
onKeyPress: function (val, e, field, options) {
field.mask(behavior.apply({}, arguments), options);
}
};
$('.phone').mask(behavior, options);
@tfpeixoto
Copy link

Boa!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment