PDA

View Full Version : Checkbox Validation Help



gareth886
09-11-2007, 11:03 AM
I'm having trouble incorporating validation for check boxes on my webstie and was wondering if anyone can help. I have sucessfully implemented validation for the text fields but can't get the check boxes to validate.

I want the validator to ensure that at least one of the 3 check boxes are checked before the form will submit.

What code should I use? My current validation code is posted below:


<script language="JavaScript" type="text/JavaScript">
function validate() {
var message='';
var count=0;
formName=mainform.Name.value;
formCompany=mainform.Company.value;
formTelephone=mainform.Telephone.value;
formEmail=mainform.mailfrom.value;
formRequest=mainform.Request.value;
formRequest2=mainform.Request2.value;
formRequest3=mainform.Request3.value;

if (formName=='') {
count=count + 1;
message=(message + count +". NAME :\t\tName Missing\n");
event.returnValue=false;
}
if (formCompany=='') {
count=count + 1;
message=(message + count +". ORGANISATION :\tOrganisation Missing \n");
event.returnValue=false;
}

if (formTelephone=='') {
count=count + 1;
message=(message + count +". TELEPHONE :\t\tTelephone No. Missing \n");
event.returnValue=false;
}
if (formEmail=='') {
count=count + 1;
message=(message + count +". EMAIL :\t\tEmail Address Missing\n");
event.returnValue=false;
}

if (count>0){
alert("You missed "+ count +" fields on this registration form.\nPlease fill in the following fields: \n\n"+ message +"\n Click OK to correct these errors");}
}
</SCRIPT>

Here's the form code:


<form action="https://*****.php" method="post" id="mainform" onsubmit="validate();">

<input name="subject" type="hidden" value="EirGrid Annual Customer Conference 2007- Online Booking" />
<input
name="realname" type="hidden" value="EirGrid Annual Customer Conference 2007- Online Booking" />
<input name="template"
type="hidden" id="template" value="emailtemplate18.htm" />
<input
name="html" type="hidden" id="html"
value="yes" />
<input name="Event_Title" type="hidden" id="Event_Title" value="EirGrid Annual Customer Conference 2007" />
<input name="Event_Date" type="hidden" id="Event_Date" value="3rd - 4th October 2007" />
<input name="Event_Venue" type="hidden" id="Event_Venue" value="Crown Plaza Hotel, Dublin" />
<input name="Pic" type="hidden" id="Pic" value="http://www.bmfconferences.com/img/xxx.gif" />
<input name="Check" type="hidden" id="Check" value="1" />

<table width="100%" border="0" cellspacing="2" summary="Date Venue Info">
<tr>
<td colspan="3" valign="top"> </td>
</tr>

<tr>
<td height="30" colspan="3" valign="top"><table width="100%" border="0" cellpadding="2" cellspacing="0" bgcolor="#F0EADA" style="border:1px solid #cccccc;" summary="User Input">
<tr valign="top">
<td width="18%" style="padding-top:6px;"><p class="topic">I wish to:</p></td>
<td>



<label>
<input type="checkbox" name="Request" id="AttendConf" value="Attend Main Conference" />
Attend the <strong>Main Conference</strong><br />
<input type="checkbox" name="Request2" id="AttendDinner" value="Attend Conference Dinner" />
Attend the <strong>Conference Dinner</strong><br />
<input type="checkbox" name="Request3" id="AttendWorkshop" value="Attend Post Conference Workshop" />
Attend the <strong>Post-Conference Workshop</strong></label> </td>
</tr>
</table></td>
</tr>
<tr>
<td width="18%">&nbsp;</td>
<td align="left" class="lightblue">&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label for="Name">Name:</label></td>
<td width="54%" align="left" class="lightblue"><input name="Name" id="Name" type="text" size="38" /> </td>
<td width="28%"><font size="1">Required</font></td>
</tr>
<tr>
<td><label for="Position">Job Title:</label></td>
<td align="left" valign="top" class="lightblue" ><input name="Position" type="text" id="Position" size="38" /> </td>
<td>&nbsp;</td>
</tr>
<tr>
<td><label for="Company">Organisation:</label></td>
<td align="left" valign="top" class="lightblue"><input name="Company" type="text" id="Company" size="38" /> </td>
<td><font size="1">Required</font></td>
</tr>
<tr>
<td><label for="Telephone">Telephone:</label></td>
<td align="left" valign="top" class="lightblue" ><input name="Telephone" type="text" id="Telephone" size="38" /> </td>
<td><font size="1">Required</font></td>
</tr>
<tr>
<td><label for="mailfrom">Email:</label></td>
<td align="left" class="lightblue" ><input name="mailfrom" type="text" id="mailfrom" size="38" /> </td>
<td><font size="1">Required</font></td>
</tr>

<tr>
<td><label for="comments">Comments?</label></td>
<td><textarea name="comments" cols="38" rows="3" id="comments"></textarea></td>
<td><font size="1">(optional)</font></td>
</tr>
<tr>
<td height="36" valign="top" class="lightblue"><br />
<br />
<br />
<br />
<br /></td>
<td height="36" colspan="2" valign="middle" class="lightblue"><input type="submit" value="Submit Your Order" name="B1" />
<span class="smallgreen"><font size="1">&lt;--
Click here to complete your Registration</font></span></td>
</tr>
<tr>
<td height="36" colspan="3" valign="middle" class="lightblue"><span class="spaced_line"><img src="https://sslrelay.com/bmfconferences.com/img/padlock2.gif" width="20" height="24" align="absmiddle" /> <font size="1"><strong><font face="Verdana, Arial, Helvetica, sans-serif">Secure Booking Area</font></strong> - This page is hosted on a 128-bit SSL secure server</font></span> </td>
</tr>
</table>
</form>
Any help would be much appreciated. I'm a noob with JavaScript.