Forum Moderators: open

Message Too Old, No Replies

Radio Button Selection Validation.

         

hellbus

12:18 pm on Oct 13, 2009 (gmt 0)

10+ Year Member



Hello. Please forgive me if this is a stupid question. I am a total noob when it comes to Java Script.

Anyway, I am trying to create a form where people have to select a radio button from 2 different tables. The tables are built via PHP, populated with info from an SQL Database.
If there is only one selection available, I have PHP place a "Checked", in the input tag of the radio button, so the user does not have to select it (since there IS only one).

The problem I have is that the code I have seems to work only when there are more than one selection. If I provide the user with the "checked" radio button, the script seems to think that nothing from that table has been selected. If I were to throw another row on the first table, and remove the "Checked" settings, everything works fine.

Here is the code I have:

<html>
<head>
<script language="JavaScript">
<!--
function radio_button_checker()
{
// set var radio_choice to false
var radio_choice_user = false;
var radio_choice_mgr = false;

// Loop from zero to the one minus the number of radio button selections
for (counter = 0; counter < user_select.user.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (user_select.user[counter].checked)
radio_choice_user = true;
}
for (counter = 0; counter < user_select.mgr.length; counter++)
{
// If a radio button has been selected it will return true
// (If not it will return false)
if (user_select.mgr[counter].checked)
radio_choice_mgr = true;
}

if (!radio_choice_mgr ¦¦ !radio_choice_user)
{
// If there were no selections made display an alert box
alert("Both a User and Manager must be selected.")
return (false);
}
return (true);
}
-->
</script>

</head>
<body>

<form method='post' name='user_select' onsubmit='return radio_button_checker()' action='index.php'>
<table class='results' border='1' cellpadding='1' cellspacing='1'><tr>

<th>Select</th><th>Name</th><th>Site</th><th>Email Address</th><th>Phone Number</th></tr>
<tr class='col1'><td><input type='radio' name='user' value='001' checked></td>
<td>Name</td>

<td>site</td>

<td>email</td>

<td>phone</td>

</tr>

</table><br/><br><form method='post' name='user_select' onsubmit='return radio_button_checker()' action='index.php'>
<table class='results' border='1' cellpadding='1' cellspacing='1'><tr>

<th>Select</th><th>Name</th><th>Site</th><th>Email Address</th><th>Phone Number</th></tr>

<tr class='col1'><td><input type='radio' name='mgr' value='002'></td>
<td>Name</td>

<td>site</td>

<td>email</td>

<td>phone</td>

</tr>

<tr class='col2'><td><input type='radio' name='mgr' value='003'></td>
<td>Name</td>

<td>site</td>

<td>email</td>

<td>phone</td>

</tr>

</table><br/><input type='submit' name='user_selected' value='Submit'>
</form>

Any help would be appreciated.

Fotiman

8:08 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Your script is assuming that user_select.user is an array. When there is only one input named "user", user_select.user is going to be a reference to that single input, not to an array. So you can't do user_select.user[counter].

You might be able to do something like this:


<script type="text/javascript">
function radio_button_checker() {
// set var radio_choice to false
var radio_choice_user = false;
var radio_choice_mgr = false;
// Loop from zero to the one minus the number of radio button selections
var userList = document.getElementsByName('user');
var mgrList = document.getElementsByName('mgr');
for (counter = 0; counter < userList.length; counter++) {
// If a radio button has been selected it will return true
// (If not it will return false)
if (userList[counter].checked) {
radio_choice_user = true;
}
}
for (counter = 0; counter < mgrList.length; counter++) {
// If a radio button has been selected it will return true
// (If not it will return false)
if (mgrList[counter].checked) {
radio_choice_mgr = true;
}
}
if (!radio_choice_mgr ¦¦ !radio_choice_user) {
// If there were no selections made display an alert box
alert("Both a User and Manager must be selected.")
return (false);
}
return (true);
}
</script>

I haven't tested it, but getElementsByName should return an array of elements, even if there is only one.

Also, note that I removed your language attribute (it's invalid and not needed) and replaced it with type (which is required to be valid), and I removed your HTML comments at the beginning and end of your script tags (this has not been needed for many years and should no longer be used).
Hope this helps.

Fotiman

8:09 pm on Oct 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Note, if you cut and paste the example above, you'll need to replace the ¦¦ with real pipe characters.

hellbus

11:19 am on Oct 14, 2009 (gmt 0)

10+ Year Member



Thank you very much. That worked great. Thank you for your patience. As I said, I am very new to Java script (or any type of scripting, for that matter). You have been a big help.