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

Keep getting the alert... 1

Status
Not open for further replies.

GUJUm0deL

Programmer
Jan 16, 2001
3,676
0
0
US
I'm having some trouble here...
The script is supposed to give an alert when no radiobuttons are selected, and it does do that, but it also gives an alert when a radio button is selected...why??

Code:
<html>
<head>

<script>
function YourChoice() {
  if(!form1.choice.checked) {
    alert(&quot;please choose one&quot;);
	return false;
  }
  return true;
}
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;form1&quot; method=&quot;post&quot; action=&quot;mailto:mmm@aol.com&quot; onSubmit=&quot;return YourChoice();&quot;>
  <input type=&quot;radio&quot; name=&quot;choice&quot; value=&quot;Tes&quot;>
  <input type=&quot;radio&quot; name=&quot;choice&quot; value=&quot;Jes&quot;>
  <input type=&quot;submit&quot; name=&quot;Submit&quot; value=&quot;Submit&quot;>
</form>
</body>
</html>
I have not failed; I merely found 100,000 different ways of not succeding...
 
Since you actually have TWO radio buttons, one of them is always unchecked, and you have to inspect each one:

function YourChoice()
{
if(!form1.choice[0].checked && !form1.choice[1].checked)
{
alert(&quot;please choose one&quot;);
return false;
}
return true;
}
 
But what if we add more radio buttons?? I have not failed; I merely found 100,000 different ways of not succeding...
 
Ironicaly I thought of doing it that way also...but I didn't...so does that mean I knew the solution or I didn't?? LOL... I have not failed; I merely found 100,000 different ways of not succeding...
 
This is not tested but should do the trick.

function YourChoice()
{
for(i=0;i<form1.choice.length;i++)
{
if(!form1.choice.checked)
{
alert(&quot;please choose one&quot;);
return false;
}
}
return true;
}
 
joeoz, nope that does the same thing as before...keeps throwing the alert no matter what's selceted...
I wrote the function like this...[
code]
<script>
function YourChoice()
{
for(i=0;i<document.form1.choice.length;i++)
{
if(!document.form1.choice.checked)
{
alert(&quot;please choose one&quot;);
return false;
}
}
return true;
}
</script>
[/code] I have not failed; I merely found 100,000 different ways of not succeding...
 
little fix go an otherwise excellent solution:

function userChoseOne()
{
for(i = 0; i < form1.choice.length; i++)
{
if(form1.choice.checked)
{
return true;
}
}
return false;
} Gary Haran
 
Joeoz is REAL close, just needed to check the radio button by index number, too. And I almost NEVER use just the letter i as an index counter because of other javascript functions possibly doing that and getting them messed up. Here, the variable ri stands for radio (button) index. If you use just i for index counters, you WILL encounter problems if you do much Javascript coding.

function YourChoice()
{
for(var ri=0;ri<form1.choice.length;ri++)
{
if(!form1.choice[ri].checked)
{
alert(&quot;please choose one&quot;);
return false;
}
}
return true;
}
 
trollacious is right even my code had that error :

function userChoseOne()
{
for(i = 0; i < form1.choice.length; i++)
{
if(form1.choice.checked)
{
return true;
}
}
return false;
} Gary Haran
 
Xutopia has the logic correct, and I missed that.

function YourChoice()
{
for(var ri=0;ri<form1.choice.length;ri++)
{
if(form1.choice[ri].checked){return true;}
}
alert(&quot;please choose one&quot;);
return false;
}
 
Also thanks trollacious I have not failed; I merely found 100,000 different ways of not succeding...
 
Can you guys help clear something up??
Why is the portion in red (the alert and the return statement) after the IF clause is closed, why can't it be beofre the IF clause ends?? Why can't it be written inside the FOR loop?? Since the FOR loop is checking if the buttons are clicked or not...

function YourChoice() {
for(var ri=0;ri<form1.choice.length;ri++) {
if(form1.choice[ri].checked) {
return true;
}
}
alert(&quot;please choose one&quot;);
return false;

} I have not failed; I merely found 100,000 different ways of not succeding...
 
here's what I use to validate if a radio button is checked

function radio_button_checker(radio)
{
// set var radio_choice to false
var radio_choice = false;

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

if (!radio_choice)
{
return false;
}

}
 
When inside a for loop if you &quot;return&quot; it stops the execution of your function altogether as your function things it came to a logical end.

A &quot;break&quot; is different where it only stops the execution of a loop (inside {}).

What this next function does is looks at all the elements, if one is checked then we know that the user checked at least one.

If you return false as soon as you find a non checked item and alert the user he needs to check at least one it is as if you told someone that he needs to be at least 5 feet tall but you only looked at the size of his feet.

Here is the correct version in simplified code style :

function YourChoice()
{
var returnValue = false;// value to send

for(var i = 0; i < form1.choice.length; i++)
{
if(form1.choice.checked)// only if it is checked
{
returnValue = true;
break;// this stops from searching anymore
}
}
// if we reach this point and never found one to be checked
// then returnValue is still its default value of false

if (returnValue == false)
{
alert(&quot;please choose one&quot;);
}

return returnValue; // this is true if one was checked or false otherwise
}

Hope this clears it up a little bit. Gary Haran
 
Since you want to check to see if one radio button is checked, as soon as you find one checked, the test returns true from the function, and that function is over. If none are checked, then the loop completes without returning, and only THEN do you know that none of the radio buttons are checked, which gives you the alert. Until you see if ANY radio button is checked, you can't be sure if you're going to have a true or false value. Since computers are stupid (but fast), you have to have the programming look at each button individually in sequence, and the first one it finds shows that the user has made one selection and the function is done.

With radio buttons, you can only have one selection checked, so any one checked qualifies.

If you want to check individual values for some reason, you can do that, too, but that wasn't what the question was here.

TheBestOfMe's function doesn't return true ever, so you'd only get a false or null statement, or possibly an error if it was true. For it to work properly, it'd have to have:

return radio_choice;

as the last statement.
 
I get it...
Thanks everyone...

PS -- trollacious, you should register your name...and become a member... I have not failed; I merely found 100,000 different ways of not succeding...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top