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

Checkbox array validation 1

Status
Not open for further replies.

LTeeple

Programmer
Aug 21, 2002
362
CA
Hi all,

I did a search, and did find some useful information, but I'm still getting a Javascript error.

I have a checkbox array, and I want to validate to ensure at least one checkbox is checked before allowing user to submit the form.

Relevant HTML code:
Code:
<script language="javascript" type="text/javascript" src="checkform.js"></script>
<form name="add" method="post" action="save.php?tableid=18" onSubmit="return CheckForm(this)">
<input type="checkbox" name="program[1]" value="Program one">
Program one<br>
<input type="checkbox" name="program[2]" value="Program two">
Program two<br>
<input type="checkbox" name="program[3]" value="Program three">
Program three<br>
<input type="checkbox" name="program[4]" value="Program four">
Program four<br>
<input type="checkbox" name="program[5]" value="Program five">
Program five<br>
<input type="checkbox" name="program[6]" value="Program six">
Program six<br>
<input type="checkbox" name="program[7]" value="Program seven">
Program seven
</form>

Relevant Javascript code: (external file - checkform.js)
Code:
function CheckForm(aForm){
  var errorString = "The following problems occurred: \r\r";
  var validHandle = true;
  obj = aForm.elements['program'];
  var txt = "";
  for (i = 0; i < obj.length; i++) {
    if (obj[i].checked) {
      txt = txt + obj[i].value + " ";
    }
  }
  if (txt == "") {
	validHandle = false;
    errorString += "Program not chosen. \r";
  }
  else {
    validHandle = true;
  }
  if (validHandle == false) { 
    errorString += "\r";              
    alert(errorString+'\nPlease make changes and submit the form again.');        
  } 
  return (validHandle == true)
}

Javascript error:
Code:
Error: obj has no properties
Source File: checkform.js
Line: 7

Why doesn't obj have properties? I thought I assigned properties to it in the line: obj = aForm.elements['program'];

Thanks for your time.

[cheers]
Cheers!
Laura
 
AFAIK, your form element names do not adhere to the W3C standards. You should not have [ or ] in element names.

That aside, you do not have an array of checkboxes, as each has a different name anyway. They would all have to have the same name to be an array.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Just a thought... If you name all your checkboxes simply "program", your code might work.

Dan

[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Okay I've made the changes, and the code works now.

Thanks for the help and explanation.

[cheers]
Cheers!
Laura
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top