I'm using AJAX and PHP to check in real time if the email is already registered in my DB, it shows a message in the DIV # response, whether the email already exists or not, so long. But if the email already exists it simply continues and accepts SUBMIT.
register.php
<input id="email" name="email" type="text" value="" placeholder="Digite seu e-mail" required>
<div id="resposta"></div>
<script language="javascript">
var email = $("#email");
email.blur(function() {
$.ajax({
url: 'verificaEmail.php',
type: 'POST',
data:{"email" : email.val()},
success: function(data) {
console.log(data);
data = $.parseJSON(data);
$("#resposta").text(data.email);
}
});
});
</script>
VerifyEmail.php
<?php
#Verifica se tem um email para pesquisa
if(isset($_POST['email'])){
#Recebe o Email Postado
$emailPostado = $_POST['email'];
#Conecta banco de dados
$con = mysqli_connect("localhost", "root", "", "academia");
$sql = mysqli_query($con, "SELECT * FROM usuarios WHERE Email = '{$emailPostado}'") or print mysql_error();
#Se o retorno for maior do que zero, diz que já existe um.
if(mysqli_num_rows($sql)>0)
echo json_encode(array('email' => 'Ja existe um usuário cadastrado com este email'));
else
echo json_encode(array('email' => 'Usuário valido.' ));
}
?>
Example, in my form, I have the field repeat the password.
<input name="senha_confirma" type="password" value="" placeholder="Confirme sua senha" required oninput="validaSenha(this)">
I'm using setCustomValidity to check if the password is the same as the first "password" field:
<script>
function validaSenha (input){
if (input.value != document.getElementById('txtSenha').value) {
input.setCustomValidity('As senhas não coincidem!');
} else {
input.setCustomValidity('');
}
}
</script>
This way the user can not give SUBMIT before correcting this. Can someone help me solve my problem with the email field?