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

Validating radiobuttons 1

Status
Not open for further replies.

btween

Programmer
Aug 7, 2003
338
US
I have a script that is supposed to validate groups of radio buttons. It works when comparing one group of buttons, but does not work when more than one group is involved.

For example:

this one works:

Code:
function checkform()
{
	if (!document.form1.Price[0].checked &&
	!document.form1.Price[1].checked &&
	!document.form1.Price[2].checked) {
		// no radio button is selected
		alert('Please select the destination country for your 3 Issue Subscription!');
		return false;
	}




	return true;
}

but this one does not:

Code:
function checkform()
{
	if (document.form1.Name[0].checked &&
	!document.form1.Price[0].checked &&
	!document.form1.Price[1].checked &&
	!document.form1.Price[2].checked) {
		// no radio button is selected
		alert('Please select the destination country for your 3 Issue Subscription!');
		return false;
	}

	return true;
}



How can I make it so i can compare the value of the field Name together with the field Price?

thanks for your help.


 
If you're just adding another condition to the if statement you can always nest it inside of the original if statement.
Code:
if (condition that I already know is working) {
   if (new condition that I want to add) {
      alert("some error message");
      return false;
   }
}

-kaht

How much you wanna make a bet I can throw a football over them mountains?
sheepico.jpg
 
It could be that naming a form field "Name" is causing problems (even though it has an uppercase "N")... try renaming it.

It the "Name" field a radio button array (i.e. does it have multiple radio buttons)? If not, remove the "[0]".

Failing that, perhaps you could post your form code, too?

Hope this helps,
Dan

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
It seems that you have only one radio button in the "Name" group (based on what you've written).

If that is the case, it MAY be that JavaScript does not treat these as arrays/collections.

You would then need to refer to document.form1.Name[0].checked as merely document.form1.Name.checked (without the index on 'Name').

Dave


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
O Time, Strength, Cash, and Patience! [infinity]
 
It was none of these issues. My error was that I had the radiobutton name set to "Name", but the id was set to "Price." This is what happens when you carelessly copy data from one place to another.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top