Google Answers Logo
View Question
 
Q: Javascript Validation of Checkboxes ( Answered 5 out of 5 stars,   0 Comments )
Question  
Subject: Javascript Validation of Checkboxes
Category: Computers > Programming
Asked by: catullus13-ga
List Price: $10.00
Posted: 06 Jul 2004 07:56 PDT
Expires: 05 Aug 2004 07:56 PDT
Question ID: 370271
I have a form, one of whose questions uses the following HTML code:

<TABLE ALIGN="center" WIDTH="390" BORDER="0" CELLPADDING="0" CELLSPACING="0">
<FORM ACTION="survey8.html" METHOD="POST" onsubmit="return checkit(this);">
<INPUT TYPE="hidden" NAME="uid" VALUE="<? echo $uid; ?>">
<TR><TD VALIGN="top" ALIGN="center">
<FONT FACE="arial,helvetica" SIZE="-1"><B>4. Pick the three (3)
extrusion equipment suppliers you would call first for a new equipment
proposal (Select three only):</B></FONT></TD>
</TR>
<TR><TD ALIGN="center">
<TABLE WIDTH="300">
<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6a" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>Davis-Standard</B></FONT></TD></TR>

<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6b" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>Gloucester</B></FONT></TD></TR>

<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6c" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>HPM</B></FONT></TD></TR>

<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6d" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>Kuhne</B></FONT></TD></TR>

<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6e" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>Merritt</B></FONT></TD></TR>

<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6f" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>Milacron</B></FONT></TD></TR>

<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6g" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>PTI</B></FONT></TD></TR>

<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6h" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>Welex</B></FONT></TD></TR>

<TR>
<TD VALIGN="bottom" ALIGN="right"><INPUT TYPE="checkbox" NAME="q6i" VALUE="1"></TD>
<TD VALIGN="bottom" ALIGN="left"><FONT FACE="arial, helvetica"
SIZE="-1"><B>Other </B></FONT><INPUT TYPE="text" NAME="q6idesc"
SIZE="15"></TD></TR>

</TABLE>
</TD></TR>
<TR><TD HEIGHT="40">&nbsp;</TD></TR>
<TR>
<TD ALIGN="center" COLSPAN="2">
<FONT FACE="arial, helvetica" SIZE="-1">
<CENTER>
<INPUT TYPE="submit" VALUE="Next >>"></FONT></CENTER>
</TD></TR></FORM>
</TABLE>

As you can see, all of the checkboxes have a different name. I already
have the Javascript validation set so that the user has to check at
least one checkbox to submit the form, and enter a company name in the
text box if the user checked "Other." Here it is:

<SCRIPT language="javascript">
<!-- HIDE
function checkit(aform)
{
    if(!(aform.q6a.checked || aform.q6b.checked || aform.q6c.checked
|| aform.q6d.checked || aform.q6e.checked || aform.q6f.checked ||
aform.q6g.checked || aform.q6h.checked || aform.q6i.checked))
    {
      window.alert("Please answer the question.");
      aform.q6a.focus();
      aform.q6a.select();
      return false;
    }
   if(aform.q6i.checked && aform.q6idesc.value.length ==0)
    { 
     window.alert("Please name other extrusion equipment supplier.");
     aform.q6idesc.focus();
     aform.q6idesc.select();
     return false;
    }
}
//-->
</SCRIPT>

Now, my problem is that the user must not select more than three (3)
checkboxes. How do I code it so that the user gets an alert window
when they try to submit with more than three checkboxes selected?

Thanks for your help!
Answer  
Subject: Re: Javascript Validation of Checkboxes
Answered By: andyt-ga on 06 Jul 2004 09:38 PDT
Rated:5 out of 5 stars
 
Hi catullus13-ga,

Thanks for your question!  Below you will find an addition to your
javascript code to check if greater than 3 checkboxes have been
selected, and if so issue a window alert on submit.  Please let me
know if you need anymore help or a clarification.

Sincerely,
Andyt-ga


<SCRIPT language="javascript">
<!-- HIDE
function checkit(aform)
{
    if(!(aform.q6a.checked || aform.q6b.checked || aform.q6c.checked
|| aform.q6d.checked || aform.q6e.checked || aform.q6f.checked ||
aform.q6g.checked || aform.q6h.checked || aform.q6i.checked))
    {
      window.alert("Please answer the question.");
      aform.q6a.focus();
      aform.q6a.select();
      return false;
    }
   if(aform.q6i.checked && aform.q6idesc.value.length ==0)
    { 
     window.alert("Please name other extrusion equipment supplier.");
     aform.q6idesc.focus();
     aform.q6idesc.select();
     return false;
    }
   var checkboxcount=0;
   for(var i=0;i<aform.elements.length;i++) 
   {
       if(aform.elements[i].checked) 
       {
       checkboxcount+=1;
       }
   }
   if(checkboxcount>3)
   {
       window.alert("Please select fewer then 3 checkboxes.");
       aform.q6idesc.focus();
       aform.q6idesc.select();
   }

}
//-->
</SCRIPT>
catullus13-ga rated this answer:5 out of 5 stars and gave an additional tip of: $2.00
It works perfectly! Thanks for all your help. You did a fantastic job.

Comments  
There are no comments at this time.

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy