/    Sign up×
Community /Pin to ProfileBookmark

How can I validate a radio button?

I would like to make sure the user selects a radio button. (as in a Yes, No set) By default none of the buttons are selected. if (strMedical == ”) or if (strMedical == ‘No’), etc. doesn’t respond. Is there a way to check if a button in a set has been selected and pop up a message if none of the buttons were selected?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@gil_davisNov 22.2002 — Radio buttons have a "checked" parameter. If the button is selected, it's "checked" parameter is true, otherwise it is false. So use the following syntax:

if (!document.formName.strMedical.checked) // not checked
Copy linkTweet thisAlerts:
@KeithPauthorNov 22.2002 — Thanks, Gil. I was able to get that working for checkboxes, but it doesn't seem to work on radio buttons. It returns as unchecked no matter what. Is there a different paramiter that is used by radio buttons?
Copy linkTweet thisAlerts:
@gil_davisNov 22.2002 — Yes, a radio button has the attribute "checked". See http://developer.netscape.com/docs/manuals/js/client/jsref/radio.htm#1193294.

Is the radio button really in an array or something?

Post the relevant parts of the form. Better yet, a link to your page.
Copy linkTweet thisAlerts:
@KeithPauthorNov 22.2002 — Yes, you were right. When there's more than one button in a set it's an array. I just added the [0] and [1] at the end and now I can detect Yes and No selections. Thanks for the link the to javascript docs. That will be a big help.
×

Success!

Help @KeithP 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.5,
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: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...