/    Sign up×
Community /Pin to ProfileBookmark

Validate Radio Button

I’m stumped! I need to do a very simple validation – did one of the radio buttons get checked (there are two)?

This is what I’m trying to do:
}
//Validate the CD Field
if(document.Order1.CD[0].checked &&
document.Order1.CD[1].checked )
{
alert(“Please choose if you wish a CD”);
document.Order1.CD.focus();//Focus on the field
return false;
}

What am I doing wrong??

Thanks for any help!

Critters

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@pyroMay 09.2003 — You are using && (and). Obviously both of the radio buttons can't be checked at the same time. Try using || (or) like this:

if (document.Order1.CD[0].checked || document.Order1.CD[1].checked )
Copy linkTweet thisAlerts:
@pyroMay 09.2003 — Umm... your code will behave no differntly from mine, besides the fact that it is checking to make sure that the radio buttons aren't checked. Due to the way that radio buttons work, only one [i]can[/i] be checked, so my code is checking if either of them are. Yours is checking if they both aren't. Same results...You find out whether or not a button had been selected....
Copy linkTweet thisAlerts:
@pyroMay 09.2003 — But, if you negate my code (or put the alert, etc code in an else), it should simply check to make sure that neither one are checked...
Copy linkTweet thisAlerts:
@pyroMay 09.2003 — Next time I will be more clear. This is the code that I was implying...

if(document.Order1.CD[0].checked || document.Order1.CD[1].checked ) {
}
else {
alert("Please choose if you wish a CD");
document.Order1.CD.focus();
return false;
}


And don't come back on and say "you could just negate it to get rid of the else loop." I already said that...
Copy linkTweet thisAlerts:
@crittersauthorMay 09.2003 — I tried the || but it didn't work, but when I used the exclamation marks with the && it worked. But I had to remove the focus because it gave me an error, but when I did, it works great now.

Thanks, guys!!

Critters
Copy linkTweet thisAlerts:
@pyroMay 09.2003 — Sorry, don't mean to be touchy. Just couldn't see why you coudn't see how the way that I posted could work. I agree, yours is more intuitive, but stand by the fact that mine will work for what he was trying to do... Peace?
Copy linkTweet thisAlerts:
@pyroMay 09.2003 — <html>
<head>
<script language="javascript" type="text/javascript">
function test() {
if(document.Order1.CD[0].checked || document.Order1.CD[1].checked ) {
}
else {
alert("Please choose if you wish a CD");
document.Order1.CD[0].focus();
return false;
}
}
</script>
</head>
<body>
<form name="Order1">
<input type="radio" name="CD" value="zero">
<input type="radio" name="CD" value="one">
<input type="button" value="test" onclick="test();">
</form>
</body>
</html>
×

Success!

Help @critters spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...