Jump to content

Radio Buttons - Checking If One Is Clicked


murfitUK
 Share

Recommended Posts

I have a form which lists some entries pulled from a database. The user has to check ONE of these. I'm trying to use javascript to check that one has been clicked before proceeding. Unfortunately, I am rubbish with js so have to rely on pinching bits of code from elsewhere!This is part of the validation script:

function validate_addsplit(){valid = true;var mess='Please correct the following errors:\n';	  ...var chosen = 0;var len = document.addsplit.rooms.length;for (i = 0; i < len; i++)  {  if (document.addsplit.rooms[i].checked)	{	chosen = 1;	}  }if (chosen == 0)  {  mess = mess + '- No account chosen\n';  valid = false;  }	  if (valid == false)  {  alert(mess);  }return valid;

The relevant part of the form is <input type=\"radio\" name=\"rooms\" value=\"{$row['id']}\" /> {$row['desc']}<br />Now, it all works so long as there are at least two radio buttons displayed. If the user doesn't select either they get the error message. But, if only one row is displayed it will always display the error even if the user chooses the radio button. I can't work out what is going wrong.Basically, I don't care which option they choose - as long as they choose one. Can someone please help.

Link to comment
Share on other sites

instead of using JS to make sure that one is checked, just check one using HTML so that way a value is already selected. You could also try putting in a hidden input with the name being rooms and the value being 'null' and have the server display an error when ever rooms == 'null'. Other than that, it would be helpful if you posted all of the content of the page instead of just the Java Script.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...