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

All checkboxes checked b4 submit...

Status
Not open for further replies.
May 9, 2000
446
GB
Hi, i have virtually no experience of javascript so could do with some help!

I have an asp which lists clients from a database. Anyway next to each client there is a checkbox. Basically I need to be able to have the forms submit button disabled until all of the checkboxes on the page have been checked / selected.

I've seen posts of how one checkbox being clicked can enable a submit button but none to make sure all are checked b4 enabling it. The number of clients listed on the page varies although i guess i can use the number of rows returned in the recordset to determine how many checkboxes there are on the page?

Cheers
 
cheers for the reply jpbrassard but there dosen't seem to be an explanation of enabling a submit button once all checkboxes on the page have been checked...
 
Hmm... true. Okay - what I'd do is make sure every checkbox has the same name (ex: 'test') then do something like:

[tt]function validateCheckbox(){
var objChkBox = document.getElementsByName('test');
var objButton = document.getElementById('submitButton');
var n = 0;
var bAllChecked = true;

for (n=0; n < objChxBox.length; n++) {
if (objChkBox[n].checked == false){
bAllChecked = false;
n = objChkBox.length; //set n to length, gets you out of the for loop
} else {
bAllChecked = true;
}
}

if (bAllChecked == true){
objButton.disabled = false;
} else {
objButton.disabled = true;
}
}

Try that and see if that helps.

HTH,
jp
 
Hi Vanillapod,

try this script:
Code:
<script>
function validate_checks(aspform)
{	  
  for (var x=0; x<parseInt(aspform.elements.length); x++)
  {
    if (aspform.elements[x].type.indexOf(&quot;checkbox&quot;) != -1)
    {
	  if (aspform.elements[x].checked == false)
	  {
	    return false;
	  }	
	}
  }
  return true;
}
</script>

then use this in your form tag
Code:
onSubmit=&quot;return validate_checks(this);&quot;

it will only allow the submit if every checkbox is checked no matter how many there are.

I hope it helps.



Travis Hawkins
BeachBum Software
travis@cfm2asp.com
 
Chers for th replys, I've tried the following:

<script>
function validate_checks(aspform)
{
for (var x=0; x<parseInt(aspform.elements.length); x++)
{
if (aspform.elements[x].type.indexOf(&quot;checkbox&quot;) != -1)
{
if (aspform.elements[x].checked == false)
{
return false;
}
}
}
return true;
}
</script>

In the head section of the page and then:

onSubmit=&quot;return validate_checks(this);&quot;

in my form tag but even with all the checkboxes on the screen checked it doesn't submit... i click the button and nothing happens...

do i have to call the checkboxes anything or can they have any name?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top