Gossamer Forum
Home : General : Internet Technologies :

Another fun javascript problem :/

Quote Reply
Another fun javascript problem :/
Hi guys,

I have 5 radio buttons (NONE of these are pre-selected).

Basically, if someone doesn't enter a rating... then I need it to pop up an alert. I know the other 2 subroutines are working fine... but this new one just doesn't seem to report an error back :/

The code is;

Code:
if (main.EditorRating.value == "" && main.DeleteReason.value == "") {
alert( "You need to set a Rating for this piece of work!");
return false;
}

The radio buttons HTML looks like;

Code:
<input type="radio" value="1" name="EditorRating">1
<input type="radio" value="2" name="EditorRating">2
<input type="radio" value="3" name="EditorRating">3
<input type="radio" value="4" name="EditorRating">4
<input type="radio" value="5" name="EditorRating">5

One of the subs that works is ;

Code:
if (main.ChangedTitle.value == "" && main.DeleteReason.value == "") {
alert( "You need to set a Title for this piece of work!");
main.ChangedTitle.focus();
return false;
}

Could this have something to do with using the radio buttons? AFAIK, these are supported in Javascript?

TIA

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!
Quote Reply
Re: [Andy] Another fun javascript problem :/ In reply to
Give this a try...

Code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<script language="javascript" type="text/javascript">
function validate() {
var iFound = 0;
for (var i = 0; i < testing.length; i++) {
if (testing.elements[i].name == "EditorRating" && testing.elements[i].checked == true) {
iFound = 1;
break;
}
}
if (iFound == 0) {
alert("Please select a rating!");
return false;
}
}
</script>

</head>

<body>

<form name="testing">

<input type="radio" value="1" name="EditorRating">1
<input type="radio" value="2" name="EditorRating">2
<input type="radio" value="3" name="EditorRating">3
<input type="radio" value="4" name="EditorRating">4
<input type="radio" value="5" name="EditorRating">5

<button onclick="return validate();">Validate</button>
</form>

</body>
</html>

There may be a better way but that's just an example I made up.