Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I do this? 1

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
US
There are at least 6 or 7 checkboxes on a page. And someone can ONLY check ONE checkbox. If they check more then one they get an al3rt message.
I can only do it if they DON'T check the checkbox.
This is what I have so far:

<script language=&quot;javascript&quot;>
function validate() {
var check = false
for (i=0; i<=6; i++) {
if (document.formsreq.APPLICATIONS(i).checked) {
check = true
}
}
if (check == true) {
return true;
}
else {
alert(&quot;You must select a form!&quot;);
return false;
}
}
</script>

Anyone see what I'm doing wrong? I have not failed; I merely found 100,000 different ways of not succeding...
 
GUJUm0deL,

heres what i would do:

function validation() {with (document.pickaformName) {
if ((elements[01].checked == false) && (elements[02].checked == false) && (elements[03].checked == false) && (elements[04].checked == false) && (elements[05].checked == false)) {
alert(&quot;You must select a form!&quot;);return false;
}
}return true;} Hope it helps ;)

~ jsLove
 
Why don't you just use radio buttons instead? <--&quot;Didn't your code work? You must have made a mistake when you pasted it.&quot; - Mark Hazen-->

If this post was useful to you, click the link below
 
jsLove, Hi. I tried you're suggestion and it didnt; work. For some reason, the script itself doesn't work anymore.
You have any other suggestions?
It should do this: If one checkbox is selected, its ok. If more then one checkbox is selected an alert pops up...
Thanks for the help... I have not failed; I merely found 100,000 different ways of not succeding...
 
Crundy -- the client wants checkboxes instead... I have not failed; I merely found 100,000 different ways of not succeding...
 
GUJUm0deL,

oops, my bad...thought ya wanted to make sure that they at least checked one...

..lemme rethink it ;) Hope it helps ;)

~ jsLove
 
Im still playing around with it and came up with this now:


<script language=&quot;javascript&quot;>
function validate() {
var check = false
for (i=0; i<=6; i++) {

for(y=0;y<=6;y++) {
if(document.formsreq.APPLICATIONS(y).checked) {
alert(&quot;only check once!&quot;);
}
}

if (document.formsreq.APPLICATIONS(i).checked) {
check = true
}
}
if (check == true) {
return true;
}
else {
alert(&quot;You must select a form!&quot;);
return false;
}
}
</script>

what I though was what if I use another for loop to check if another checkbox is selceted. But there's a problem with this way: no matter even if one is selected the alert stills pops up.
I have not failed; I merely found 100,000 different ways of not succeding...
 
GUJUm0deL,

...well, its simple and lengthy, but ya might try something like this?

function validation() {with (document.pickaformName) {
if ((elements[01].checked == false) && (elements[02].checked == false) && (elements[03].checked == false))
{alert(&quot;You must select a form!&quot;);return false;}

else if (
((elements[01].checked == false) &&
(elements[02].checked == true) &&
(elements[03].checked == true)) ||

((elements[01].checked == true) &&
(elements[02].checked == false) &&
(elements[03].checked == true)) ||

((elements[01].checked == true) &&
(elements[02].checked == true) &&
(elements[03].checked == false)))

{alert(&quot;You may only select one!&quot;);return false;}
}return true;}

Hope it helps ;)

~ jsLove
 
jsLove. I just tried out you're solution and still no luck. Now the alert keeps poping out no matter if one checkbox is selcted or all are selected...
I thought of using a double for loop. But the problem I see with that is that the alert pops up 6 times before the form can be sent...
This is what I came up with now:

<html>
<head>

<script language=&quot;javascript&quot;>
function validate() {
var check = false
for (i=0; i<=6; i++) {
for(y=0;y<=6;y++) {
if(document.formsreq.APPLICATIONS(y).checked) {
alert(&quot;only check once!&quot;);
}
}

if (document.formsreq.APPLICATIONS(i).checked) {
check = true
}
}
if (check == true) {
return true;
}
else {
alert(&quot;You must select a form!&quot;);
return false;
}
}
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>

<form id=formsreq name=formsreq>
0. <input type=checkbox id=APPLICATIONS><br>
1. <input type=checkbox id=APPLICATIONS><br>
2. <input type=checkbox id=APPLICATIONS><br>
3. <input type=checkbox id=APPLICATIONS><br>
4. <input type=checkbox id=APPLICATIONS><br>
5. <input type=checkbox id=APPLICATIONS><br>
6. <input type=checkbox id=APPLICATIONS><br>
<input type=button value=&quot;Submit&quot; onClick=&quot;validate()&quot;>
</form>
</body>
</html>
I have not failed; I merely found 100,000 different ways of not succeding...
 
Try this:
Code:
function validate() {
var check = 0;
for(y=0;y<document.formsreq.APPLICATIONS.length;y++) {
   if(document.formsreq.APPLICATIONS(y).checked) {
       check++;
}
if (check > 1) {
    alert('Only check one');
    return false;
}
else
    return true;
}
</script>

You could also use this to test that at least one checkbox is checked if you need to.
 
Watch the brackets, I missed one around the for loop.

function validate() {
var check = 0;
for(y=0;y<document.formsreq.APPLICATIONS.length;y++) {
if(document.formsreq.APPLICATIONS(y).checked)
check++;
}
if (check > 1) {
alert('Only check one');
return false;
}
else
return true;
}
</script>

This should work better.
 
bvallet, Hi. Thanx for the reply. But you're suggestion gives me an 'error on page'...and the script doesn't work... I have not failed; I merely found 100,000 different ways of not succeding...
 
bvallet, EUREKA!! It worked...thanx alot man...I was getting a headache because I couldn't figure it out...
see I tried to use a double for loop, which did give me that alert, but that alert kept repeating itself over and over again...
thanx a bunch... I have not failed; I merely found 100,000 different ways of not succeding...
 
bvallet, one more question:

If I wanted to use you're script for two diff. set of checkbox choices how can I do that?? I tried alot diff. ways...I tried to duplicate the function with diff. variables, tried adding more function and nothing...you have any ideas?? I have not failed; I merely found 100,000 different ways of not succeding...
 
bvallet --
Hi. Ok, scratch the last post. I played with the code and figured out how to implement that for 2 or 3 or even more diff. checkboxes.
This is the code:

<html>
<head>

<script>
function validate1() {
var check = 0;

for(y=0;y<document.formsreq.APPLICATIONS.length;y++) {
if(document.formsreq.APPLICATIONS(y).checked)
check++;
}
if (check > 1) {
alert('Only check one');
return false;
}
else
return true;

}
</script>
<script>
function validate2() {
var check2 = 0;

for(i=0;i<document.formsreq.TRY.length;i++) {
if(document.formsreq.TRY(i).checked)
check2++;
}
if (check2 > 1) {
alert('Only check one');
return false;
}
else
return true;

}
</script>

<script>
function validate() {
document.formsreq.APPLICATIONS.clicked = validate1()
document.formsreq.TRY.clicked = validate2()
}
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>

<form name=formsreq onClick=&quot;validate()&quot;>
0. <input type=checkbox id=APPLICATIONS><br>
1. <input type=checkbox id=APPLICATIONS><br>
2. <input type=checkbox id=APPLICATIONS><br>
3. <input type=checkbox id=APPLICATIONS><br>
4. <input type=checkbox id=APPLICATIONS><br>
5. <input type=checkbox id=APPLICATIONS><br>
6. <input type=checkbox id=APPLICATIONS><br>
<br><br>
0. <input type=checkbox id=TRY><br>
1. <input type=checkbox id=TRY><br>
2. <input type=checkbox id=TRY><br>
3. <input type=checkbox id=TRY><br>
4. <input type=checkbox id=TRY><br>
5. <input type=checkbox id=TRY><br>
6. <input type=checkbox id=TRY><br>
<input type=button value=&quot;Submit&quot;>
</form>
</body>
</html>
I have not failed; I merely found 100,000 different ways of not succeding...
 
As long as all the checkboxes had the same requirements (i.e. only one box checked per group), you could have one version of the function and pass the checkbox object to it:
Code:
<script>
function validate(field) {
  var check = 0;
  for(y=0;y<field.length;y++) {
     if(field[y].checked)
         check++;
  }
  if (check > 1) {
      alert('Only check one');
      return false;
  }
  else
      return true;
}
</script>

<form name=formsreq onSubmit=&quot;
  if(!validate(checkboxa))
    return false;
  if(!validate(checkboxb))
    return false;
  /*repeat for any other checkboxes you need to check, 
    or build the validate() function call into an over-all 
    form validation routine*/
&quot;>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top