duanjian9148 2018-12-18 10:23
浏览 39

当在表单中插入错误信息时,可替代控制器中的echo函数

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"; } 
  }
  • 写回答

1条回答 默认 最新

  • dongtan6336 2018-12-18 10:35
    关注

    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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化