Click to See Complete Forum and Search --> : two functions, one submit
coloradoguy
12-31-2003, 03:15 AM
Hello,
I used Dreamweaver to validate a simple form. Now I added checkboxes to the form and need to ensure someone checks at least one. I found the code for doing this, but can't figure out how to make it work. Thanks to anyone who might be able to help.
Thanks,
Will
Here's my head code. The first is what DW added. The second is what I'm trying to add for the checkboxes.
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
//-->
</script>
<script type="text/javascript" language="JavaScript">
<!--
function checkCheckBoxes(theForm) {
if (
theForm.CHECKBOX_1.checked == false &&
theForm.CHECKBOX_2.checked == false &&
theForm.CHECKBOX_3.checked == false)
{
alert ('You didn\'t choose any of the checkboxes!');
return false;
} else {
return true;
}
}
//-->
</script>
And here's my form action:
<form action="phpformmail/formmail.php" method="post" name="form1" id="form1" onSubmit="MM_validateForm('first_name','','R','last_name','','R','Company','','R','email','','RisEmail');retur n document.MM_returnValue">
fredmv
12-31-2003, 03:22 AM
Welcome to the forums.<script type="text/javascript">
//<![CDATA[
function validateForm()
{
var f = document.forms[0].elements;
if(f[0].checked == false && f[1].checked == false && f[2].checked == false)
{
alert('You must check atleast one checkbox.');
return false;
}
return true;
}
//]]>
</script><form action="phpformmail/formmail.php" method="post" onsubmit="return validateForm();" />
<div>
<ul>
<li>
<input type="checkbox" id="c1" />
<label for="c1">Checkbox 1</label>
</li>
<li>
<input type="checkbox" id="c2">
<label for="c2">Checkbox 2</label>
</li>
<li>
<input type="checkbox" id="c3">
<label for="c3">Checkbox 3</label>
</li>
</ul>
<input type="submit" value="Submit" />
</div>
</form>
coloradoguy
01-01-2004, 06:34 AM
Thank you, but I can't get this to work. Here's the complete code for my page. Can you see what's wrong with it?
Thanks so much,
Will
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_validateForm() { //v4.0
var i,p,q,nm,test,num,min,max,errors='',args=MM_validateForm.arguments;
for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=MM_findObj(args[i]);
if (val) { nm=val.name; if ((val=val.value)!="") {
if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
if (p<1 || p==(val.length-1)) errors+='- '+nm+' must contain an e-mail address.\n';
} else if (test!='R') { num = parseFloat(val);
if (isNaN(val)) errors+='- '+nm+' must contain a number.\n';
if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
min=test.substring(8,p); max=test.substring(p+1);
if (num<min || max<num) errors+='- '+nm+' must contain a number between '+min+' and '+max+'.\n';
} } } else if (test.charAt(0) == 'R') errors += '- '+nm+' is required.\n'; }
} if (errors) alert('The following error(s) occurred:\n'+errors);
document.MM_returnValue = (errors == '');
}
function checkCheckBoxes(theForm) {
if (
theForm.Request_for_Quotation[].checked == false &&
theForm.Immediate_need.checked[] == false &&
theForm.Future_Purchase.checked[] == false &&
theForm.File_Information.checked[] == false &&
theForm.General_Information.checked == false)
{
alert ('You didn\'t choose any of the checkboxes!');
return false;
} else {
return true;
}
}
//-->
</script> </tr>
</table>
<table width="760" height="400" border="0" cellpadding="0" cellspacing="0">
<tr valign="top">
<td width="251" background="images/layout/leftbar.gif"> <table width="250" border="0" cellpadding="10" cellspacing="0" class="sidebar">
<tr>
<td width="50"> </td>
<td width="200"><img src="images/layout/bullet.gif" width="9" height="9" hspace="5" align="absmiddle"><a href="home.htm">HOME</a><br>
<img src="images/layout/bullet.gif" width="9" height="9" hspace="5" align="absmiddle"><a href="products.htm">PRODUCTS</a><br>
<img src="images/layout/bullet.gif" width="9" height="9" hspace="5" align="absmiddle"><a href="atc.htm">ADVANCED
TECHNOLOGY<br>
<img src="images/spacer.gif" width="18" height="8" border="0">CENTER</a>
<br>
<img src="images/layout/bullet.gif" width="9" height="9" hspace="5" align="absmiddle"><a href="support.htm">CUSTOMER
SUPPORT<br>
</a><img src="images/layout/bullet.gif" width="9" height="9" hspace="5" align="absmiddle"><a href="edm.htm">EDM.USA
DIVISION</a><br>
<img src="images/layout/bullet.gif" width="9" height="9" hspace="5" align="absmiddle"><a href="demo_usedequipment.htm">DEMO/USED
EQUIPMENT</a><br>
<img src="images/layout/bullet.gif" width="9" height="9" hspace="5" align="absmiddle"><a href="news.htm">NEWS
& EVENTS</a><br> <img src="images/layout/bullet.gif" width="9" height="9" hspace="5" align="absmiddle"><a href="contact.htm">CONTACT
US </a></td>
</tr>
<tr>
<td width="50"> </td>
<!-- InstanceBeginEditable name="SideBar" -->
<td width="200">This is the sidebar text, that can be changed on each
page. Here is a <a href="#">sample link</a> that goes nowhere.</td>
<!-- InstanceEndEditable --></tr>
</table>
</td>
<!-- InstanceBeginEditable name="Body" -->
<td width="509"> <table width="100%" border="0" cellpadding="15" cellspacing="0" class="body">
<tr>
<td valign="top"><p><img src="images/computerafterhours.jpg" alt="Contact Price Equipment Company" width="150" height="150" hspace="10" vspace="5" border="2" align="right">All
of our contact info including the form below will go here. The user
will be provided with a form link to go straight to the form if
he wishes. </p>
</td>
</tr>
</table>
<fieldset align="center">
<legend align="left"><font color="#999999" size="3" face="Verdana, Arial, Helvetica, sans-serif"><strong><font color="#666666">Customer
Inquiry Form</font></strong></font></legend>
<form action="phpformmail/formmail.php" method="post" name="form1" id="form1" onSubmit="checkCheckBoxes(theForm);MM_validateForm('first_name','','R','last_name','','R','Company','','R','em ail','','RisEmail');return document.MM_returnValue">
<table width="455" border="0" align="center" cellpadding="0" cellspacing="5">
<tr>
<td colspan="4" valign="baseline"><div align="center"><font color="#CC0000">Note:
Items in red are mandatory</font></div></td>
</tr>
<tr>
<td colspan="4" valign="baseline"><div align="center"> <br>
<br>
</div></td>
</tr>
<tr>
<td width="137" valign="baseline"> <div align="right"><font color="#CC0000">First
name:</font></div></td>
<td width="274"><input type="text" name="first_name"></td>
<td width="9" valign="baseline"> </td>
<td width="10"> </td>
</tr>
<tr>
<td valign="baseline"><div align="right"><font color="#CC0000">Last
name:</font></div></td>
<td><input type="text" name="last_name"></td>
<td valign="baseline"> </td>
<td> </td>
</tr>
<tr>
<td valign="baseline"><div align="right"><font color="#CC0000">Company:</font></div></td>
<td><input type="text" name="Company"></td>
<td valign="baseline"> </td>
<td> </td>
</tr>
<tr>
<td valign="baseline"> <div align="right">Title:</div></td>
<td><input type="text" name="Title"></td>
<td valign="baseline"> </td>
<td> </td>
</tr>
<tr>
<td height="30" valign="baseline"> <div align="right">Street Address:</div></td>
<td><input type="text" name="Street_address"></td>
<td valign="baseline"> </td>
<td> </td>
</tr>
<tr>
<td valign="baseline"> <div align="right">City:</div></td>
<td><input type="text" name="City"></td>
<td valign="baseline"> </td>
<td> </td>
</tr>
<tr>
<td valign="baseline"><div align="right">State:</div></td>
<td><input type="text" name="State"></td>
<td valign="baseline"> </td>
<td> </td>
</tr>
<tr>
<td valign="baseline"><div align="right">Zip code:</div></td>
<td><input type="text" name="Zip code"></td>
<td valign="baseline"> </td>
<td> </td>
</tr>
<tr>
<td valign="baseline"><div align="right"><font color="#CC0000">Email:</font></div></td>
<td><input type="text" name="email"></td>
<td valign="baseline"> </td>
<td> </td>
</tr>
<tr>
<td valign="baseline"> <div align="right"><font color="#CC0000">Phone
(inc. area code):</font></div></td>
<td><input type="text" name="Phone"></td>
<td valign="baseline"> </td>
<td> </td>
</tr></table>
<table width="420" align="center">
<tr>
<td colspan="4" valign="baseline"></td>
</tr>
<tr>
<td colspan="3"><div align="center"><strong><font color="#CC0000">Reason
for contact:</font> </strong>Check all that apply</div></td><tr>
<td colspan="2"> <div align="left">
<input type="checkbox" name="Request_for_Quotation[]" value="Request for Quotation"> Request for Quotation</div></td>
<td width="255"> <div align="left"> <input type="checkbox" name="Immediate_need[]" value="Immediate need">
Immediate Need</div></td>
</tr><tr><td colspan="2"> <div align="left">
<input type="checkbox" name="Future_Purchase[]" value="Future Purchase">
Future Purchase</div></td>
<td> <div align="left">
<input type="checkbox" name="File_Information[]" value="File Information">
File Information</div></td>
</tr>
<tr>
<td colspan="2"> <div align="left">
<input type="checkbox" name="General_Information[]" value="General Information">
General Information</div></td>
<textarea name="comments" cols="30" rows="6" id="textarea2"></textarea></td>
</tr>