Form validation error

0

What I need to do is when the email is not valid because of the alert in javascript I wanted to show a message over the form in question that showed that the email was invalid!

HTML CODE      

           <input type="email" class="form-control" name="email" id="email" 
   placeholder="Email" data-rule="email" data-msg="Introduza um email 
 válido"REQUIRED />

          <div class="validation"></div>
        </div>

PHP VALIDATION

 if(isset($_POST["contact"]))
  {

 if (filter_var($email, FILTER_VALIDATE_EMAIL)) {

 $novocontacto = "insert into contacto(nomecompleto, email, assunto, 
  mensagem) VALUES ('$nomecomp', '$email', '$assunto' , '$mensagem')";
 $novocontacto_run = mysqli_query($conn,$novocontacto);

 echo 
'<script type="text/javascript">alert("Mensagem enviada com sucesso!") 
 </script>';
  }

  else 
 {
 echo '<script type="text/javascript">alert("Email inválido")</script>';
  }

    }
    
asked by anonymous 24.05.2018 / 18:29

2 answers

1

The best option would be to use the .validate jquery plugin as mentioned above, this plugin allows you to specify a set of rules for the validation content messaging and real-time validation, this example illustrates this well.

You can set the .validate to send your form via ajax if everything is correct.

    
24.05.2018 / 18:47
0

Hello, one possible solution without restructing the whole code is to add a div on top of the form and php returns the script by adding the message.

html code

// ...
<div class="mensagem"></div>
// formulário
// ...

php validation

 echo '<script type="text/javascript">document.querySelector(".mensagem").innerHTML = "email inválido"; </script>';
    
24.05.2018 / 18:45