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!

Validate multiple checkboxes ok but fails for one

Status
Not open for further replies.

mat41

Programmer
Mar 7, 2004
91
AU
Hi there JS gurus, please help....

This code works well to validate multiple checkboxes that are all named the same which is what I want. The array of checkboxes is dynamic, sometime there will only be one. In this situation if I check this one checkbox it throws the 'You have not ticked any options....'. I would like checked = true to fire in this situation. Im having a headache of a time getting it to work. your help would be appreciated?

var checked = false;
var field = myForm.selectedDocs;
for(var j = 0; j < field.length; j++)
{
if(field[j].checked == true)
{
checked = true;
break;
}
}
if(!checked)
{
alert("You have not ticked any options. At least one must be selected to proceed!")
return false;
}
 
You need to determine what you are getting from the selectedDocs element, and act accordingly.

When you have more than one input present you'll get a nodeList. (An Array of the inputs for all intents and purposes), but when you are dealing with a single element you get just that, an element, so the "length" property is meaningless as there is only one.

Code:
[tab][b][COLOR=#0000FF]var[/color][/b] field [COLOR=#990000]=[/color] myForm[COLOR=#990000].[/color]selectedDocs[COLOR=#990000];[/color]
[tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color]field[COLOR=#990000].[/color][b][COLOR=#000000]toString[/color][/b][COLOR=#990000]()[/color] [COLOR=#990000]==[/color] [COLOR=#FF0000]'[object NodeList]'[/color][COLOR=#990000])[/color]
[tab][COLOR=#FF0000]{[/color]
[tab][tab][tab][i][COLOR=#9A1900]//Do the for loop here...[/color][/i]
[tab][COLOR=#FF0000]}[/color]
[tab][b][COLOR=#0000FF]else[/color][/b] [b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color]field[COLOR=#990000].[/color][b][COLOR=#000000]toString[/color][/b][COLOR=#990000]()[/color] [COLOR=#990000]==[/color] [COLOR=#FF0000]'[object HTMLInputElement]'[/color][COLOR=#990000])[/color]
[tab][COLOR=#FF0000]{[/color]
[tab][tab][tab][i][COLOR=#9A1900]//Since its a single element, just check its "checked" property to see if its checked or not.[/color][/i]
[tab][tab][tab][b][COLOR=#0000FF]if[/color][/b][COLOR=#990000]([/color]field[COLOR=#990000].[/color]checked[COLOR=#990000]==[/color][b][COLOR=#0000FF]true[/color][/b][COLOR=#990000])[/color]
[tab][tab][tab][COLOR=#FF0000]{[/color]
[tab][tab][tab][tab][i][COLOR=#9A1900]//Do whatever[/color][/i]
[tab][tab][tab][COLOR=#FF0000]}[/color]
[tab][COLOR=#FF0000]}[/color]





----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi there - Sorry I missed this post somehow. Thank you for your answer however I am confused what to use in place of [object NodeList] and [object HTMLInputElement]

Thanking you
 
Nothing. You use that exactly. You want to check what type of object you are getting. Either an [object NodeList] or a [object HTMLInputElement].

And leave the square brackets in place. The toString function call returns exactly those strings for each type. So that's what you want to check against.

----------------------------------
Phil AKA Vacunita
----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.

Web & Tech
 
Hi there - it never enters the if or the elseif regardless of how many checkboxes are on the page. I have placed alerts there to identify this.

I have changed my original code to this which does work. Its probably a bit amateurish thought I would expect:

Code:
   if (document.getElementById('selectedDocs').checked)
   {
       //this is here to handle the situation where there is only one checkbox being displayed
   }
   else
   {
      var checked = false; 
	  var field = myForm.selectedDocs; 
	  for(var j = 0; j < field.length; j++)
	  { 
		 if(field[j].checked == true)
		 { 
			checked = true; 
			break; 
		 } 
	  } 
	  if(!checked)
	  { 
		 alert("You have not ticked any options.  At least one must be selected to proceed!") 
		 return false; 
	  }
   }
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top