PDA

View Full Version : Javascript for form error


jonirvine
01-11-2001, 11:25 AM
Hi,

here's a problem for you all... any help would be appreciated.

I am setting up a form where the user has to select 3 options from a set of 6 checkboxes.

Is it possible using javascript that if they select 4 a pop up box will appear telling them that they can't select it because they have already selected 3?

If so, I'd love to know exactly how to do it. Here's the form code:

<BLOCKQUOTE><font size="1" face="Verdana, Arial">code:</font><HR><pre>
&lt;P&gt;Please select 3 from below:
&lt;P&gt;&lt;input type=checkbox name="agree1" value="AA"&gt;The faster the better&lt;br&gt;
&lt;input type=checkbox name="agree2" value="BB"&gt;Maximum space for maximum efficiency&lt;br&gt;
&lt;input type=checkbox name="agree3" value="CC"&gt;Newest is best&lt;br&gt;
&lt;input type=checkbox name="agree4" value="DD"&gt;Value for money&lt;br&gt;
&lt;input type=checkbox name="agree5" value="EE"&gt;Quality before quantity&lt;br&gt;
&lt;input type=checkbox name="agree6" value="FF"&gt;The right tool for the right job&lt;br&gt;
[/code]

Cheers in advance,

Jon

------------------
Web Design, development and hosting.
Contact: jon@dustnet.co.uk www.yorkdesigns.co.uk (http://www.yorkdesigns.co.uk)
Newest Project: www.101tutorials.com (http://htpp://www.101tutorials.com)

Dr. Web
01-11-2001, 02:13 PM
without writing the code, here is a simple solution........

use a hidden field in your form, and set the value to 0 (zero)
if the value of any checkbox =checked, set the value of your hidden field to +1, if it is unchecked set the value to -1, then do a onmouseup, call javascript function to check the number of the hidden field. if the number of the hidden field exceeds three, set the value of the last checkbox checked to unchecked, this should also set the hidden field back to 3. that wasy you can only check 3...not 4.
now, for writing the code............

jonirvine
01-11-2001, 04:51 PM
Sorry Dr, you've lost me....lol

My javascript knowledge is minimal.

Any code clues?

kdjoergensen
01-11-2001, 05:42 PM
&lt;script language="javascript"&gt;
&lt;!--
function countEm(){
var count = 0;
for (var i=1;i&lt;=6;i++){
myObj = eval("document.myform.agree"+i);
if (myObj.checked == true) { count++ }
}
if (count &gt; 3) { alert('only 3'); return false;}
}
//--&gt;
&lt;/script&gt;

&lt;form name="myform"&gt;
&lt;input type=checkbox name="agree1"
value="AA" onclick="countEm()"&gt;The faster the better&lt;br&gt;
&lt;input type=checkbox name="agree2" value="BB" onclick="countEm()"&gt;Maximum space for maximum efficiency&lt;br&gt;
&lt;input type=checkbox name="agree3" value="CC" onclick="countEm()"&gt;Newest is best&lt;br&gt;
&lt;input type=checkbox name="agree4" value="DD" onclick="countEm()"&gt;Value for money&lt;br&gt;
&lt;input type=checkbox name="agree5" value="EE" onclick="countEm()"&gt;Quality before quantity&lt;br&gt;
&lt;input type=checkbox name="agree6" value="FF" onclick="countEm()"&gt;The right tool for the right job&lt;br&gt;
&lt;/form&gt;

--------------------------------------------------------------------------------

jonirvine
01-11-2001, 07:26 PM
Thanks!

Worked a treat!