Skip to content

Instantly share code, notes, and snippets.

@klebergraciasoares
Created June 28, 2013 12:30
Show Gist options
  • Save klebergraciasoares/5884325 to your computer and use it in GitHub Desktop.
Save klebergraciasoares/5884325 to your computer and use it in GitHub Desktop.
<input type="text" name="telefone" id="telefone" maxlength="15" />
function mascara(o, f) {
v_obj = o;
v_fun = f;
setTimeout(execmascara, 1);
}
function execmascara() {
v_obj.value = v_fun(v_obj.value)
}
function mtel(v) {
v=v.replace(/\D/g,"");
v=v.replace(/^(\d{2})(\d)/g,"($1) $2");
v=v.replace(/(\d)(\d{4})$/,"$1-$2");
return v;
}
function id(el) {
return document.getElementById(el);
}
window.onload = function() {
var $telefone = id('telefone');
$telefone.onkeyup = function() {
mascara( this, mtel );
}
$telefone.onblur = function() {
var v = this.value;
if( v.indexOf('(11)') !== -1 && v.length === 14) {
this.value = v.replace(/\((\d{2})\) (\d{4})-(\d{4})/g,'($1) 9$2-$3');
}
}
}
@klebergraciasoares
Copy link
Author

Show de bola

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