Skip to content

Instantly share code, notes, and snippets.

@clebersonfalk
Created February 15, 2019 18:54
Show Gist options
  • Save clebersonfalk/625c6bec1fa31ad5fef369d267e3c025 to your computer and use it in GitHub Desktop.
Save clebersonfalk/625c6bec1fa31ad5fef369d267e3c025 to your computer and use it in GitHub Desktop.
Mask input phone number with 10 or 11 digits
$(document).on('keypress', '.telefones', function () {
applyMask();
});
function applyMask(){
$('.telefones').each(function(index, elem){
var fone = $(this).val();
fone = fone.replace(/\D/g, '');
if (fone.slice(0,4) == '0800') {
$(this).unmask();
$(this).mask('0000-000-0000');
}else if (fone.slice(0,4) == '4004') {
$(this).unmask();
$(this).mask('0000-0000');
}else{
if (fone.length > 10) {
$(this).unmask();
$(this).mask("(00) 00000-0000");
} else {
$(this).unmask();
$(this).mask("(00) 0000-00009");
}
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment