dongxin2734 2014-04-01 10:39
浏览 22
已采纳

JavaScript验证问题

My java script validation is not working for my form. If ionsubmit="return(validate());"ut alert("fswefefew"); at the top of the tage I get the alert but the if I take it out it just sends my form with no validation.

<form name="Contact" onSubmit="return validate()" METHOD="POST" ACTION="reply.php">


<div class="Row">
   <div class="Lable">Full Name:</div> <!--End of Lable-->
   <div class="input">
<input type="text" id="fullname" class="detail" name="fullname" placeholder="Full Name" />
    </div> <!--End input-->
</div> <!--End row-->


   <br />
   <div class="Row">
   <div class="Lable">Email Address:</div> <!--End of Lable-->
   <div class="input">
<input type="text" id="emailaddress" class="detail" name="emailaddress"    placeholder="Email Address" />
</div> <!--End input-->
</div> <!--End row-->

       <br />
        <div class="Row">
  <div class="Lable">Your Message:</div> <!--End of Lable-->
      <div class="input">
     <textarea id="comment" name="comment" class="mess" placeholder="Your Message">   </textarea>
</div> <!--End input-->
</div> <!--End row--> 

   <br />
<div class="Row">
<div class="Lable">Select your gender:</div> <!--End of Lable-->
   <div class="input">   
  <input type="radio" name="gender" value="male"  id="gender_male" />
   <label for="gender_male"/>Male
<input type="radio" name="gender" value="female" id="gender_female" />
   <label for="gender_female" />Female
   </div> <!--End input-->
</div> <!--End row--> 




<br />
  <div class="Row">
  <div class="Lable">Please select your age:</div>
  <!--End of Lable-->
   <input type="radio" name="age" id="range1" value="18-25" />
  18-25 
  <input type="radio" name="age" id="range2" value="26-33" />26-33 
  <input type="radio" name="age" id="range3" value="24-40" />34-40 
  <input type="radio" name="age" id="range4" value="40+" />40+<br />
 </div><!--Row-->

  <br />
  <div class="Row">
<div class="Lable">Select 3 products that you are interested in hearing about:</div> 
<!--End of Lable-->
<input type="checkbox" name="Interested[]" id="protien" value="protien" />Protien
<input type="checkbox" name="Interested[]" id="creatine" value="creatine" />Creatine<br>
<input type="checkbox" name="Interested[]"  id="bcaa" value="bcaa" />BCAA
<input type="checkbox" name="Interested[]" id="power drinks" value="powerdrinks" />Power Drinks<br />
</div><!--Row-->





<br />
<div class="Row">
<div class="Lable">Where did you hear about us?</div> <!--End of Lable-->
<select>
<option value="" />Nothing
<option value="Google Search" selected="selected" id="heard[]" name="heard[]" /> Search
<option value="Word of mouth" name="heard[]" id="heard[]" /> Word of mouth 
<option value="Newspaper" name="heard[]" id="heard[]" /> Newspaper
<option value="Magazine" name="heard[]" id="heard[]" /> Magazine
</select>
 </div><!--Row-->



 <br />
 <div class="submit">
 <input type="submit" id="send" Name="send" value="Send" />
  </div><!--End of submit-->

 <div class="Clear">
  <input type="reset" id="clear" Name="Clear" value="Clear" />
 </div>





</form>

And this is my javascript

 <script type="text/javascript">

 <!--
 function validate()
  {
alert("Please enter your name");
  //Check to see if name has been entered - the form is named fb_form
  if (Contact.fullname.value == "")
  {
     alert("Please enter your name");
     return false;
     } 
   else
    {
    alert("Please enter your email correctly");
    }

    //This section checks there is an '@' in the email address string
    if (Contact.emailaddress.value.indexOf('@') < 1 )
      {  
      alert("Please enter your email correctly");
      return false;
    }


     if (Contact.comment.value =="" )
       {  
      alert("Please enter a message");
      return false;
     }



     //This next section sees if one of the checkboxes has been checked 
    if (((Contact.gender_male.checked) || (Contact.gender_female.checked)) == false)
   {
      alert("Please tell us your gender");
      return false;
    }

      if (((Contact.range1.checked) || (Contact.range2.checked)  ||       (Contact.range3.checked)  || (Contact.range4.checked)) == false)
     {
       alert("Please tell us your age range");
       return false;
    }
     //All is OK, then return true

        if (Contact.protien.checked || Contact.creatine.checked  || Contact.bcaa.checked  || Contact.powerdrinks.checked)
     {
       alert("good");

      }
     else
     {
       alert("bad");
      return false; 
    }



      if (Contact.heard.selectedIndex < 1 )
      {
       alert("Please rate our site");
      return false;
     } 

     return true;
   }

  //-->
   </script>
  • 写回答

1条回答 默认 最新

  • dtpa98038 2014-04-01 10:55
    关注

    There is a problem in your java script code you have missed document in your syntax

    document.Form.Name.value
    

    For your checkbox validations try this code:

     var options = document.getElementsByName("Interested[]");
    if(options[0].checked==false && options[1].checked==false && options[2].checked==false) {
            alert('Please check at least one of the options.');
            return false;
        }
        return true; 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型
  • ¥15 求学软件的前人们指明方向🥺
  • ¥50 如何增强飞上天的树莓派的热点信号强度,以使得笔记本可以在地面实现远程桌面连接
  • ¥20 双层网络上信息-疾病传播