2014-06-27 120 views
0

我有這個簡單的腳本來驗證一些輸入並顯示錯誤消息。 這裏是我的JSFiddle http://jsfiddle.net/2kJVU/JQuery在jsfiddle上工作,但不在我的服務器上

DOM被加載,嵌入腳本。該代碼部分工作,但電子郵件始終顯示在我的網站上,即使電子郵件字段填寫正確。 這是我的JS:

$(document).ready(function(){ 
    $('#registreren').click(function(){ 
     var Email = $('#Email').val(); 
     var Postcode =$('#Postcode').val(); 

     $("#add_err2").html(""); 

     if(Email == "") { 
      $("#add_err2").css({"border-radius":"5px", "background":"#fd7777", "border":"1px solid #ff0000"}); 
      $("#add_err2").html("Vul a.u.b. een E-mail adres in <br>"); 
     } 
     else if(!validateEmail(Email)){ 
      //$("#add_err2").css({"border-radius":"5px", "background":"#ff4e4e", "border":"1px solid #ff0000"}); 
      $("#add_err2").append("Vul a.u.b. een geldig E-mail adres in <br>"); 
     } 

     if(Postcode == "") { 
      //$("#add_err2").css({"border-radius":"5px", "background":"#ff4e4e", "border":"1px solid #ff0000"}); 
      $("#add_err2").append("Vul a.u.b. een Postcode in <br>"); 
     } 
     else if(Postcode.length < 4){ 
      //$("#add_err2").css({"border-radius":"5px", "background":"ff4e4e", "border":"1px solid #ff0000"}); 
      $("#add_err2").append("Vul a.u.b. een geldige Postcode in <br>"); 
     } else { 
      // send backend service; 
      $.ajax({ 
       type: "POST", 
       url: "./postcodecheck.php", 
       data: "registreren=true&Email="+Email+"&Postcode="+Postcode, 
       success: function(html){  
        if(html=='true') { 
         $("#add_err2").hide(); 
         window.location="./registreren.php"; 
        } 
        else { 
         $("#add_err2").css({"border-radius":"5px", "background":"#496999", "border":"1px solid #174385", "width":"350px;", "color":"#ffffff"}); 
         $("#add_err2").html("Helaas is BoxCloud nog niet beschikbaar in uw regio. Als u op de hoogte wilt blijven van nieuwe regio’s, meld u dan a.u.b. via de onderstaande balk aan voor de nieuwsbrief van BoxCloud"); 
        } 
       }, 
      }); 
      function validateEmail(email) { 
       var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[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); 
      } 
      return false; 
     }; 
    }); 
}); 

HTML:

<form id="klant_worden" action="" method="POST"> 
    <fieldset id="inputs"> 
     <input id="Email" type="email" name="Email" class="inputs" placeholder="Uw e-mail adres"> 
     <input id="Postcode" type="text" name="Postcode" class="inputs" placeholder="Uw postcode (1234AB)"> 
     <!-- <input type="submit" class="submitclient" id="registreren" value="Registreren" name="registreren" onclick="validateForm();"> --> 
     <input type="button" class="submitclient" id="registreren" value="Registreren" name="registreren"> 
    </fieldset> 
</form> 

<section class="err" id="add_err2"><br></section> 

這是我的服務器上工作正常,但有沒有。爲什麼?

編輯:

真人版可以在這裏找到(首次登錄框): http://www.boxcloud.nl/site/

+1

任何錯誤控制檯? –

+1

您的'電子郵件消息'在ajax響應的條件評估中被觸發。這意味着你的PHP腳本正在執行數據的返回,這是打嗝的地方。它與你的jQuery無關。由於這與電子郵件有關,我只能假定你正在使用「郵件」功能。但是沒有可能的方式讓我們在沒有看到代碼和郵件日誌的情況下回答這個問題。 – Ohgodwhy

+0

也驗證電子郵件未定義 –

回答

1

驗證電子郵件應該是別的外循環:http://jsfiddle.net/2kJVU/3/

$(document).ready(function() { 
    $('#registreren').click(function() { 
     var Email = $('#Email').val(); 
     var Postcode = $('#Postcode').val(); 

     $("#add_err2").html(""); 

     if (Email === "") { 
      $("#add_err2").css({ 
       "border-radius": "5px", 
        "background": "#fd7777", 
        "border": "1px solid #ff0000" 
      }); 
      $("#add_err2").html("Vul a.u.b. een E-mail adres in <br>"); 
     } else if (!validateEmail(Email)) { 

      $("#add_err2").append("Vul a.u.b. een geldig E-mail adres in <br>"); 
     } 

     if (Postcode === "") { 
      $("#add_err2").append("Vul a.u.b. een Postcode in <br>"); 
     } else if (Postcode.length < 4) { 
      $("#add_err2").append("Vul a.u.b. een geldige Postcode in <br>"); 
     } else { 
      // send backend service; 
      $.ajax({ 
       type: "POST", 
       url: "./postcodecheck.php", 
       data: "registreren=true&Email=" + Email + "&Postcode=" + Postcode, 
       success: function (html) { 
        if (html == 'true') { 
         $("#add_err2").hide(); 
         window.location = "./registreren.php"; 
        } else { 
         $("#add_err2").css({ 
          "border-radius": "5px", 
           "background": "#496999", 
           "border": "1px solid #174385", 
           "width": "350px;", 
           "color": "#ffffff" 
         }); 
         $("#add_err2").html("Helaas is BoxCloud nog niet beschikbaar in uw regio. Als u op de hoogte wilt blijven van nieuwe regio’s, meld u dan a.u.b. via de onderstaande balk aan voor de nieuwsbrief van BoxCloud"); 
        } 
       } 
      }); 


     } 
    }); 

    function validateEmail(email) { 
     var re = /^(([^<>()[\]\\.,;:\[email protected]\"]+(\.[^<>()[\]\\.,;:\[email protected]\"]+)*)|(\".+\"))@((\[[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); 
    } 


}); 
相關問題