Mascara Celular (9XXXX-XXXX)
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript" src="jquery-1.9.1.js"></script>
<script src="jquery.maskedinput.js" type="text/javascript"></script>
<script type="text/javascript">
//Testando o jQuery
$(document).ready(function() {
alert("Olá Mundo");
});
function Mascara(campo) {
function trata(valor, isOnBlur) {
valor = valor.replace(/\D/g, "");
valor = valor.replace(/^(\d{2})(\d)/g, "($1)$2");
if (isOnBlur) {
valor = valor.replace(/(\d)(\d{4})$/, "$1-$2");
} else {
valor = valor.replace(/(\d)(\d{3})$/, "$1-$2");
}
return valor;
}
campo.onkeypress = function (evt) {
var code = (window.event) ? window.event.keyCode : evt.which;
var valor = this.value
if (code > 57 || (code < 48 && code != 8)) {
return false;
} else {
this.value = trata(valor, false);
}
}
campo.onblur = function () {
var valor = this.value;
if (valor.length < 13) {
this.value = ""
} else {
this.value = trata(this.value, true);
}
}
campo.maxLength = 14;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox runat="server" ID="txtTelefone" MaxLength="14" onKeyDown="Mascara(this);"
onKeyPress="Mascara(this);" onKeyUp="Mascara(this);" />
<input type="text" name="celular" onkeypress="mascara(this, '## #####-####')" maxlength="13">
</form>
</body>
</html>