...

View Full Version : MY Validation code doesn't work for radiobuttons - why?



JVRudnick
12-09-2004, 10:12 PM
This is my very simple validation script for an ASP form. It works (and thanks due here to you all for helping me 2 years ago) fine, but I suddenly now find that it will not work for a radiobutton.

Why?

Here's the script I'm trying to get to work...for a trio of radio buttons

<script LANGUAGE="JavaScript">
<!-- HIDE FROM OTHER BROWSERS

function validate(outputForm)
if (outputForm.radioValue.value == "")
{

alert("The Value of the Truck is required. Thank you.");
outputForm.radioValue.focus();
return (false);
}
return (true);
// STOP HIDING FROM OTHER BROWSERS -->
</script>

???

Jim

Brandoe85
12-10-2004, 12:13 AM
Are you checking to see if the radio is selected? then you want


if (!outputForm.radioValue.checked)
{
//do something
}

Basscyst
12-10-2004, 12:55 AM
Yes, a radio button or checkbox's value does not change (generally) when it is selected. It is whether or not it is selected that you want to check. Which the code above does just fine. Just thought I'd explain a bit further.

When you execute

var x=document.form.checkbox.checked;

x holds the value of either true or false.

so now you can do


if(x==true)
{
alert("Selected");
}
else
{
alert("Not selected");
}


Basscyst

Kor
12-10-2004, 02:08 PM
trio of radio buttons

If they have all the same name (to be able to let only a single posibility to check), than to reach one button you have to circle as through an array, as the

document.forms[0].elements['radioname'] is a collection of elements, not a single element.

JVRudnick
12-10-2004, 03:03 PM
Okay, I get it, I think. It's not correct to check for a "value" -- I have to check that a selection was made....

So here's the script, now modified to do that -- right?

function validate(outputForm)
if (outputForm.radioValue.checked== "")
{
alert("A Truck Value Selection is required. Thank you.");
outputForm.radioValue.focus();
return (false);
}
return (true);

Will that work? And won't it work for ANY number of radiobuttons, 2 or 20?

Jim

Kor
12-10-2004, 03:40 PM
You have not read my replay. And you have to look after the boolean checked, not after the value.



var r = document.forms[0].elements['radioValue'];//the radio buttons as a collection
var param = 0;
for(var i=0;i<r.length;i++){
if (r[i].checked){param++}
}
if(param==0){
alert("A Truck Value Selection is required. Thank you.");
return false}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum