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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

5 different named checkbox, have to make sure one is checked

Status
Not open for further replies.

Pirellio

Programmer
Aug 25, 2008
18
US
I have
5 different named checkboxs, have to make sure at least one is checked
on submit

Any Ideas



 

This is the code I have

<script language="javascript">


function validateform(){
if (<cfloop query="theItems">!document.#theItems.theVar#.checked || </cfloop>){
alert('You must check at least one box!');
}
}
</script>
 
If you want to solve this client-side then perhaps posting client-side code would help?

However, as you clearly have access to server-side scripting, why not do server-side validation (either instead of or as well as client-side)?

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
This is making me check all the checkboxs or none at all

can anyone help?

<script language="javascript">
function validateform(){
if (
document.getElementById('RegularBox').checked
||

document.getElementById('SeniorBox').checked
||

document.getElementById('Special1').checked
||

document.getElementById('Special2').checked
||

document.getElementById('Special3').checked
||

document.getElementById('Special4').checked
||

document.getElementById('Special5').checked

) {
alert('You must check at least one item!');
return false;
}
return true;
}
</script>
 
Well yes - that's what your code is asking for. It's saying if either "RegularBox" is checked or "SeniorBox" is checked... or "Special5" is checked, then you must check at least one item.

Surely you want to be making sure that none of them are checked rather than making sure any of them are checked before issuing the alert?

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
Ok maybe I am confusing myself - I just want to make sure one box is checked yes - this doesn't work either

<script language="javascript">
function validateform(){
if (
!document.getElementById('RegularBox').checked
||

!document.getElementById('SeniorBox').checked
||

!document.getElementById('Special1').checked
||

!document.getElementById('Special2').checked
||

!document.getElementById('Special3').checked
||

!document.getElementById('Special4').checked
||

!document.getElementById('Special5').checked

) {
alert('You must check at least one item!');
return false;
}
return true;
}
</script>
 
I mean it checks them but all of the boxs have to be checked in order for the form to be submitted,

I just want to make sure one box is checked.

 
Hi

Oops. You are right, the code is wrong. You should negate the whole expression instead of negating each part separately. I would rewrite it like this :
Code:
function validateform()
{
  if (
    document.getElementById('RegularBox').checked ||
    document.getElementById('SeniorBox').checked ||
    document.getElementById('Special1').checked  ||
    document.getElementById('Special2').checked  ||
    document.getElementById('Special3').checked  ||
    document.getElementById('Special4').checked  ||
    document.getElementById('Special5').checked         
  ) return true;

  alert('You must check at least one item!');
  return false;
}


Feherke.
 
Cool thanks

This is what I ended up with

if (<cfloop query="theItems">
document.getElementById('#theItems.theVar#').checked
<cfif (theItems.CurrentRow NEQ theItems.RecordCount)>||</cfif>
</cfloop>) {
return true;
}
alert('You must check at least one item!');
return false;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top