2012-10-10 169 views
0

我正在發送一封電子郵件的聯繫表單,但我在確保人們實際上正在輸入數據時遇到問題(我知道我需要進行更深入的驗證,但我想要以確保它的工作第一)。當用戶點擊發送時,javascript會檢查以確保填寫字段。如果是這樣,那麼它將在服務器端再次驗證,然後發送消息(這工作正常),否則它將顯示一個警報,它不應該發送消息。
但是,使用這個JavaScript,如果一個字段爲空,它將顯示錯誤消息,然後發送電子郵件。我在這裏做錯了什麼?javascript電子郵件驗證問題

<script type="text/javascript"> 
       function validate() { 
        var x=document.forms['mailcontact']['name'].value; 
        var y=document.forms['mailcontact']['email'].value; 
        var z=document.forms['mailcontact']['subject'].value; 
        if (x == '' || x == null || x == ' name') { 
         alert("You need to enter a name"); 
        } 
        else if (y == '' || y == null || y == ' email') { 
         alert("You need to enter your email"); 
        } 
        else if (z == '' || z == null || z == ' subject') { 
         alert("You need to enter a subject line"); 
        } 
        else { 
         document.forms['maincontact'].submit(); 
        } 
       } 
      </script> 

      <form method="POST" action="mail.php" id="mailcontact"> 
       <input value=" name" size="25" id="contactname" type="text" name="name" onblur="if(this.value=='') this.value=' name';" onfocus="if(this.value==' name') this.value='';" /> 
       <input value=" email" size="25" id="contactemail" type="text" name="email" onblur="if(this.value=='') this.value=' email';" onfocus="if(this.value==' email') this.value='';" /> 
       <input value=" subject" size="25" id="contactsubject" type="text" name="subject" onblur="if(this.value=='') this.value=' subject';" onfocus="if(this.value==' subject') this.value='';" /> 
       <textarea name="message"> message</textarea> 
       <input onclick="validate()" value="validate_then_send" type="image" src="../images/sendbutton.png" style=" width: 200px; border: none; position: relative; left: 360px;" /> 
      </form>​ 
+0

使用'的onclick = 「返回的validate();」'你的發送按鈕,讓你的'的validate()'函數返回真正的成功或假失敗。 – Jeremy

回答

0
<script type="text/javascript"> 
       function validate() { 
        var x=document.forms['mailcontact']['name'].value; 
        var y=document.forms['mailcontact']['email'].value; 
        var z=document.forms['mailcontact']['subject'].value; 
        if (x == '' || x == null || x == ' name') { 
         alert("You need to enter a name"); 
        } 
        else if (y == '' || y == null || y == ' email') { 
         alert("You need to enter your email"); 
        } 
        else if (z == '' || z == null || z == ' subject') { 
         alert("You need to enter a subject line"); 
        } 
        else { 
         document.forms['maincontact'].submit(); 
        } 
       } 
      </script> 

      <form method="POST" action="mail.php" id="mailcontact"> 
       <input value=" name" size="25" id="contactname" type="text" name="name" onblur="if(this.value=='') this.value=' name';" onfocus="if(this.value==' name') this.value='';" /> 
       <input value=" email" size="25" id="contactemail" type="text" name="email" onblur="if(this.value=='') this.value=' email';" onfocus="if(this.value==' email') this.value='';" /> 
       <input value=" subject" size="25" id="contactsubject" type="text" name="subject" onblur="if(this.value=='') this.value=' subject';" onfocus="if(this.value==' subject') this.value='';" /> 
       <textarea name="message"> message</textarea> 
       <input onclick="validate();return false;" value="validate_then_send" type="image" src="../images/sendbutton.png" style=" width: 200px; border: none; position: relative; left: 360px;" /> 
      </form>​