Validating Email with Jquery and Ajax

Asked

Viewed 6,021 times

1

I own a form where I perform login to access a restricted area of my site, but now I need to validate the typed email, I tried to change the script that I currently have and have not been able to perform the validation.

What I got 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 validates the login:

  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.');
}

2 answers

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 working: JS Bin

2


I use it like 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.