Created
February 15, 2019 18:54
-
-
Save clebersonfalk/625c6bec1fa31ad5fef369d267e3c025 to your computer and use it in GitHub Desktop.
Mask input phone number with 10 or 11 digits
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$(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