0

I'm coding a website and I have a form. For example, when the user is writing an email in the email input and he writes "benjamin" there's automatically a window saying it's not valid (even before submitting) same if he writes "benjamin@". However if he inserts "benjamin@gmail" no window is shown and the user can submit the form. The problem os that "benjamin@gmail" is not a valid mail adress so the controller does not accept it and the user is redirected to a blank page showing "invalid e-mail" because of the echo function of the controller. How can I find an alternative for the user not to be redirected to this page?

Here how it looks like in the html file:

 <div>
      <label for="email">Adresse mail :</label>
      <input type="email" name="email" id="email" placeholder="josephine.dupont@gmail.com" required/>
    </div>

and here is the controller part:

function pageInscription2() {
    if ($_POST["nom"] && $_POST["prenom"] && $_POST["email"] && $_POST["telephone"] 
      && $_POST["cle"] && $_POST["adresse"] 
      && $_POST["code_postal"] && $_POST["pays"] 
      && $_POST["mdp"] && $_POST["mdp2"])
    {

        $nom = htmlspecialchars($_POST["nom"]);
        $prenom = htmlspecialchars($_POST["prenom"]);
        $email = htmlspecialchars($_POST["email"]);
        $telephone = htmlspecialchars($_POST["telephone"]);
        $cle = htmlspecialchars($_POST["cle"]);
        $adresse = htmlspecialchars($_POST["adresse"]);
        $code_postal = htmlspecialchars($_POST["code_postal"]);
        $pays = htmlspecialchars($_POST["pays"]);
        $mdp = htmlspecialchars($_POST["mdp"]);
        $mdp2 = htmlspecialchars($_POST["mdp2"]);

        if (filter_var($email, FILTER_VALIDATE_EMAIL))
        {

          if ($mdp==$mdp2)
          {

         insererUtilisateur($nom, $prenom, $email, $telephone, $cle, $adresse, $code_postal, $pays, $mdp);

        require "vues/inscription2.php";

      } 
      else {
          echo "Vous n'avez pas saisi les mêmes mot de passe";
    } }
    else {
        echo "Email invalide";
    } }
  else {
    $erreur = "Tous les champs doivent être complétés"; } 
  }
  • Oh sorry, the window is shown once the user submti, however it stays on the form for everything until "benjamin@gmail" and then it's sumbited and redirected to the blank page show 'invalid email" – Benjamin DontBlockMe Dec 18 '18 at 10:27
  • 1
    _“The problem os that "benjamin@gmail" is not a valid mail adress”_ - oh yes but it is. It might not _work_ on the public internet, but that has nothing whatsoever to do with whether the address itself is valid. https://stackoverflow.com/questions/20573488/why-does-html5-form-validation-allow-emails-without-a-dot – misorude Dec 18 '18 at 10:29
  • @misorude ok thank you i resolved the problem, however i still have the same problem if the user inputs two different passwords. i'll try to find a forum – Benjamin DontBlockMe Dec 18 '18 at 10:35

1 Answers1

0

You try regulare expression :

       <script>
            function testemail(email) {
              var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
              return re.test(email);
            }


        $("form").submit(function(e){

        var email=$('#email').val();
           var check=testemail(email);
         if(check != true){
          alert('wrong email');
            e.preventDefault()
                }


        });
        </script>
HamzaNig
  • 1,019
  • 1
  • 10
  • 33