我的java脚本验证不适用于我的表单。如果ionsubmit =" return(validate());" ut alert(" fswefefew");在Tage的顶部,我得到警报但是如果我把它拿出来它只是发送我的表格而没有验证。
<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>
这是我的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>
答案 0 :(得分:1)
您的java脚本代码中存在一个问题,即您在语法中遗漏了文档
document.Form.Name.value
对于您的复选框验证,请尝试以下代码:
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;