How to validate mail with Jquery and Ajax [duplicate]

1

I have form where I log in to access a restricted area of my website, but now I need to validate the typed e-mail, / em> that I currently have and could not perform validation.

What I have is this:

<!-- language: lang-js -->

    $(document).ready(function(){
    $('#MsgErro').hide(); 
    $('#formLogin').submit(function(){  
        var login = $('#email').val();  
        var senha = $('#senha').val();
        $.ajax({        
            url:"login.php",            
            type:"post",                
            data: "login="+login+"&senha="+senha,   
            success: function (result){         
                if (result == 1) {                      
                    location.href = 'restrito.php'  
                } else {
                    $('#MsgErro').show();
                }
            }
        })
        return false;   
    })
})

The PHP that does the login validation:

  mysql_connect('***', '***','***')or die();    
  mysql_select_db('***');  

  $login = mysql_real_escape_string($_POST['login']);
  $senha = mysql_real_escape_string($_POST['senha']);

  $sql = "SELECT * FROM usuarios_catalogos WHERE email='".$login."' AND senha='".md5($senha)."'"; 

  $resultados = mysql_query($sql) or die (mysql_error());
  $res = mysql_fetch_array($resultados); //

    if (@mysql_num_rows($resultados) == 0){
        echo 0; 
    } else {
        echo 1; 

        if (!isset($_SESSION))
        session_start();

        $_SESSION['usuarioID'] = $res['id'];        
        $_SESSION['nomeUsuario'] = $res['nome'];
        $_SESSION['email'] = $res['email']; 
        exit;   
    }

Error Message:

<h4 style=" font-family: 'Open Sans Condensed', sans-serif; font-size: 16px;" align="center" id="MsgErro">Usuário ou senha informados incorretamente</h4>

And what I tried to do was this:

var emailFilter=/^.+@.+\..{2,}$/;
var illegalChars= /[\(\)\<\>\,\;\:\\/\"\[\]]/
// condição
if(!(emailFilter.test(email))||email.match(illegalChars)){
    $("MsgErro").show()
    .text('Por favor, informe um email válido.');
}
    
asked by anonymous 30.11.2015 / 14:15

2 answers

2

I often use this:

var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
if (!filter.test(form.email.value)) {
    form.email.focus();
    $("MsgErro").show()
    .text('Por favor, informe um email válido.');
    return false;
}
    
30.11.2015 / 16:57
2

Well, if you just need to validate the email you can do so:

var email = document.getElementById('email');

if (validaEmail(email.value)) {
  document.write('valido.');
  // Chama o Ajax e realiza ações com o email validado.
} else {
  document.write('invalido.');
  // mostra msgErro dizendo que o email do cliente não é valido.
}

function validaEmail(email) {
  var regex = /^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
  return regex.test(email);
}
<input type="text" value="[email protected]" id="email">

See it working: JS Bin

    
30.11.2015 / 16:03