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!

Help with form, checkbox required.

Status
Not open for further replies.

jpopuk

Programmer
Mar 27, 2006
6
GB
<script language="JavaScript">
<!--
function formCheck(formobj){
// Enter name of mandatory fields
var fieldRequired = Array("Title", "FirstName", "LastName", "Email", "Terms");
// Enter field description to appear in the dialog box
var fieldDescription = Array("Title", "First Name", "Last Name", "Email", "Terms");
// dialog message
var alertMsg = "Please complete the following fields:\n";

var l_Msg = alertMsg.length;

for (var i = 0; i < fieldRequired.length; i++){
var obj = formobj.elements[fieldRequired];
if (obj){
switch(obj.type){
case "select-one":
if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == "Please select"){
alertMsg += " - " + fieldDescription + "\n";
}
break;

case "select-multiple":
if (obj.selectedIndex == -1){
alertMsg += " - " + fieldDescription + "\n";
}
break;

case "text":
case "textarea":
if (obj.value == "" || obj.value == null){
alertMsg += " - " + fieldDescription + "\n";
}
break;


default:
}


if (obj.type == undefined){
var blnchecked = false;
for (var j = 0; j < obj.length; j++){
if (obj[j].checked){
blnchecked = true;
}
}
if (!blnchecked){
alertMsg += " - " + fieldDescription + "\n";
}
}
}
}

if (alertMsg.length == l_Msg){
return true;
}else{
alert(alertMsg);
return false;
}


}

// -->
</script>

Can anyone see why in my form when field isn't clicked it works fine except with checkbox's.

Would be most grateful for any help.

Paul
 
Your switch does not cater for single checkboxes right now.

Are you talking about single or multiple checkboxes with the same name?



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
It's just for one checkbox, do I need to implement some extra code in for it to work?
 
Yes - add a 'checkbox' case to your switch statement. You also don't have one for radio buttons, but perhaps this is not an issue for you.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Ok, I am not 100% sure how to do this as I am relitvely new to it. How would I go about doing this?

Thanks
 
Pick one of the existing 'case' lines, let's say the 'select-multiple' one, and paste this code above it:

Code:
case "checkbox":
	if (obj.checked) {
		// do something
	}
break;

If you want to do something if the checkbox isn't checked, then change "(obj.checked)" to "(!obj.checked)".

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Ok, I am a little confused. I don't understand the '// do something' part. Sorry, what should I put here?

"(!obj.checked)". ???

Really appreciate the help.

Paul
 
That would be the part where you do whatever it is you want to do when validation fails.

We have no idea what that is, and if you don't then this thread is going to die very quickly!

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Tech Snippets & Info:
 
Sorted now. Many thanks for all your help Dan!

Cheers,

Paul
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top