none
Validar Email jQuery 2 RRS feed

  • Pergunta

  • Esse script valida o campo TextEmail, (ajuda do amigo Vitor Mendes)

    ------------------------------------------------

    <script type="text/javascript">

            function isValidEmailAddress(TextEmail) {
                var controle = $('[id$=' + TextEmail + ']').val();
                var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
                return pattern.test(controle);
            };
            </script>

        <script type="text/javascript">

            function Validacoes() {        
                    if (!isValidEmailAddress('TextEmail')) {
                        alert('Email invalido !'); return false;
                    }
            };     
        </script>

    ------------------------------------------------

    Gostaria que ele validasse apenas se estivesse algo digitado no campo TextEmail, tentei assim mas nao deu certo


            function Validacoes() {
                var valor = $('[id$=' + TextEmail + ']').val();
                if (valor.toString != null) {
                    if (!isValidEmailAddress('TextEmail')) {
                        alert('Email invalido !'); return false;
                    }
                }
            };     

    terça-feira, 17 de abril de 2012 12:12

Respostas

  • Boa vida, fiz um teste aqui e funcionou:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
    
            function isValidEmailAddress(TextEmail) {
                var controle = $('[id$=' + TextEmail + ']').val();
                var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
                return pattern.test(controle);
            };
    
            function ValidarTextBox(nomeControle, mensagemErro) {
                var controle = $('[id$=' + nomeControle + ']');
                if (!controle || controle.val() == "" || controle.val() == null) {
                    alert(mensagemErro);
                    if (controle) controle.focus();
                    return false;
                } else {
                    return true;
                }
            }
    
            function Validacoes() {
                if (!ValidarTextBox('txtEmail', 'Preencha o campo de e-mail !')) {
                    return false;
                } else {
                    if (!isValidEmailAddress('txtEmail')) {
                        alert('Email invalido !');
                        return false;
                    }
                }
            };     
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        E-mail:
        <asp:TextBox runat="server" ID="txtEmail"></asp:TextBox>
        <br />
        <asp:Button runat="server" OnClientClick="return Validacoes();" Text="Validar" />
        </form>
    </body>
    </html>
    


    Vitor Mendes | Seu feedback é muito importante para todos!
    Visite o meu site: http://www.vitormendes.com.br/

    • Marcado como Resposta Álvaro Luiz terça-feira, 17 de abril de 2012 22:21
    terça-feira, 17 de abril de 2012 20:48

Todas as Respostas

  • Tente desta maneira, uma função que uso em conjunto com JQuery:

    function ValidarControle(nomeControle, mensagemErro) {
        var controle = $('[id$=' + nomeControle + ']');
        if (!controle || controle.val() == "" || controle.val() == null) {
            alert(mensagemErro);
            if (controle) controle.focus();
            return false;
        } else {
            return true;
        }
    }

    Ai é só usar:
    if(ValidarControle('txtEmail', 'Digite um email'))
    {
    	//Sua rortina para validar email aqui
    }




    Vitor Mendes | Seu feedback é muito importante para todos!
    Visite o meu site: http://www.vitormendes.com.br/

    terça-feira, 17 de abril de 2012 12:36
  • olá Vitor, coloquei assim mas nao funcionou

     <script type="text/javascript">
            function Validacoes() {
                var controle = $('[id$=' + TextEmail + ']');
                if (!controle || controle.val() == "" || controle.val() == null) {
                    if (!isValidEmailAddress('TextEmail')) {
                        alert('Email invalido !'); return false;
                    } 
                }
            };     
        </script>

    terça-feira, 17 de abril de 2012 12:54
  • Tentei assim também mas nada

    <script type="text/javascript">
            function Validacoes() {
                var controle = $('[id$=' + TextEmail + ']');
                if (!controle || controle.val() == "" || controle.val() == null) {
                    return true;
                }
                else if (!isValidEmailAddress('TextEmail')) {
                    alert('Email invalido!'); return false;
                }
            };     

    terça-feira, 17 de abril de 2012 13:27
  • Boa vida, fiz um teste aqui e funcionou:

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
        <title></title>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js" type="text/javascript"></script>
        <script type="text/javascript">
    
            function isValidEmailAddress(TextEmail) {
                var controle = $('[id$=' + TextEmail + ']').val();
                var pattern = new RegExp(/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
                return pattern.test(controle);
            };
    
            function ValidarTextBox(nomeControle, mensagemErro) {
                var controle = $('[id$=' + nomeControle + ']');
                if (!controle || controle.val() == "" || controle.val() == null) {
                    alert(mensagemErro);
                    if (controle) controle.focus();
                    return false;
                } else {
                    return true;
                }
            }
    
            function Validacoes() {
                if (!ValidarTextBox('txtEmail', 'Preencha o campo de e-mail !')) {
                    return false;
                } else {
                    if (!isValidEmailAddress('txtEmail')) {
                        alert('Email invalido !');
                        return false;
                    }
                }
            };     
        </script>
    </head>
    <body>
        <form id="form1" runat="server">
        E-mail:
        <asp:TextBox runat="server" ID="txtEmail"></asp:TextBox>
        <br />
        <asp:Button runat="server" OnClientClick="return Validacoes();" Text="Validar" />
        </form>
    </body>
    </html>
    


    Vitor Mendes | Seu feedback é muito importante para todos!
    Visite o meu site: http://www.vitormendes.com.br/

    • Marcado como Resposta Álvaro Luiz terça-feira, 17 de abril de 2012 22:21
    terça-feira, 17 de abril de 2012 20:48
  • Obrigado Vitor, funcionou apenas precisei mudar:

    function ValidarTextBox(nomeControle, mensagemErro) {
                var controle = $('#<%= TextEmail.ClientID %>').val();
                if (!controle || controle == "" || controle == null) {
                    alert(mensagemErro);
                    if (controle) controle.focus();
                    return false;
                } else {
                    return true;
                }
            }

    terça-feira, 17 de abril de 2012 22:21