Help to validate form before sending

0

I have the following function that validates me if the email entered in the database exists and works well for me

var emailExistente = false;

//validar email en el registro
$("#correo_usuario").change(function(){

  var email = $("#correo_usuario").val();

  var datos = new FormData();
  datos.append("validarEmail", email);

  $.ajax({
    url:"views/modulos/ajax.php",
    method:"POST",
    data: datos,
    cache: false,
    contentType: false,
    processData: false,
    success:function(respuesta){

      if(respuesta == 0){

        $("label[for='correo_usuario'] span").html('<p class="text-danger">Este email ya existe en la base de datos</p>');
        $("input[id='correo_usuario']").parent().addClass('has-danger');

        emailExistente = false;
      }

      if(respuesta == 1){

        $("label[for='correo_usuario']").parent().addClass('has-success');
        $("label[for='correo_usuario'] span").html('');
        $("input[id='correo_usuario']").parent().removeClass('has-danger');
        $("input[id='correo_usuario']").parent().addClass('has-success');

        emailExistente = true;
      }

    }

  });

});

Now the problem is that although the mail exists and I put a false like this is executing the submit, I would appreciate if you could help me create a function or something that prevents me from sending the submit if the email exists

    
asked by jorgnv 17.08.2017 в 23:00
source

1 answer

0

First, in the comments they ask you where the form is valid and you have placed:

function validarRegistroU() { 
  //valido el nombre
  if (emailExistente = false) {
    alert("El correo ingresado ya esta en uso") return false;
  }
}

But the IF has only one equal sign (=) instead of two (==) although it is redundant.

If emailExisting = true, then you can use:

if(emailExistente) // Si es true, entonces haz ...

Review that IF.

    
answered by 18.08.2017 в 15:39