Javascript Question

Status
Not open for further replies.
For a text field

if (theForm.address.value == "")
{
alert("Please enter your company's \"Address\".");
theForm.address.focus();
return (false);
}

For a pull down

if (theForm.country.selectedIndex == 0)
{
alert("Please select the \"Country\" in which your company is located.");
theForm.country.focus();
return (false);
}

For a radio button

if (theForm.model.status == "")
{
alert("Please select the \"model\" you would like to try risk free.");
theForm.model.focus();
return (false);
}


I am pretty sure the problem has to do with status in theForm.model.status

I have tried value in place of status but that doesn't work either.

What do you think?

bloome
 
I'm assuming that since you are using radios, your given options are 3 or less. (more than 3 and it's more efficient using a dropdown). It's funny that the value or status doesn't work for radios... don't you love JS??

One sure way that works is to check every option to see if they are checked. So given the following:

<form name="theForm">
<input type="radio" name="model" value="1">1
<input type="radio" name="model" value="2">2
<input type="radio" name="model" value="3">3
</form>

You would write:

if (theForm.model[0].checked == "false" && theForm.model[1].checked == "false" && theForm.model[2].checked == "false")
{
alert("Please select the \"model\" you would like to try risk free.");
theForm.model.focus();
return (false);
}

[This message was edited by FuriMonkey on October 15, 2001 at 18:22.]
 
Hmmm.... try this then:

Declare this variable at the top:
var radioSelect = ""

----------------------------------------------------

if (radioSelect = "")
{
alert("Please select the \"model\" you would like to try risk free.");
theForm.model.focus();
return (false);
}
---------------------------------------------------

<form name="theForm">
<input type="radio" name="model" onclick="radioSelect = '1'">1
<input type="radio" name="model" onclick="radioSelect = '2'">2
<input type="radio" name="model" onclick="radioSelect = '3'">3
etc.....
</form>
 
in onclick="radioSelect= 'x'"
1,2,3 is the values that you want to send to your cgi script. The other numbers outside of the <> are what you see after the radio button.

When you're sending the values to your cgi script, you have to use the radioSelect variable instead of the form value. You can try adding value="" in the input tags and see if that works and use the radioSelect variable just for validation.
 

Cupcake

Ars Tribunus Militum
2,688
just to help clarify things. You must work with radio buttons differently since there will be several with the same name (unlike text and other fields where there is a unique name for every element).

You need to loop through all the radio buttons and determine if
the CHECKED attribute is TRUE... then get the value. To
validate, you'd just set a flag that indicates something has
been checked.


+cup
 
the only way they could pick more than one is if you had at least two different named sets of radio buttons.

that's the whole point of radio buttons.

they radio. like tune in tokyo.

Well, he didn't say he wanted someone to pick more than one... I'm assuing that there is only one he wants someone to pick. If you want somone to pick more than one from 1 set, you need to use checkboxes.
 

Cupcake

Ars Tribunus Militum
2,688
i'd do it this way.

<html>
<head>
<SCRIPT Language="JavaScript">
function checkIt(){
var didjaclick=-1
for (i=0;i<document.form1.length;i++)
{
if (document.form1.elements.checked)
{
didjaclick=i
}
}
if (didjaclick==-1)
{
alert("why haven't you clicked a button? all the kool kids are.");
return false;
}
}
</SCRIPT>
</head>
<body>
<form name="form1" onSubmit="checkIt()">
<INPUT TYPE="radio" NAME="rad1" value="1">1
<INPUT TYPE="radio" NAME="rad1" value="2">2
<INPUT TYPE="radio" NAME="rad1" value="3">3
<INPUT TYPE="radio" NAME="rad1" value="4">4
<input type="submit">
</form>
</body>
</html>
 
FuriMonkey is right that I have a bunch of radio button all named the same thing and I want the use to select one.

If there is a javascript that will loop through all radio buttons with the same name and determine which is checked I am all ears, as long as it requires less work then adding the onclick statement to all of my radio buttons.

Cupcake can you document what you proposed in the first place.
 
Status
Not open for further replies.