Created
June 28, 2013 12:30
-
-
Save klebergraciasoares/5884325 to your computer and use it in GitHub Desktop.
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
<input type="text" name="telefone" id="telefone" maxlength="15" /> |
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
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'); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Show de bola