Code for validating radio button in javascript

I've combed a ton of the pages on here, and still am incapable to get my explicit validator to work.

Basically, when the submit button is clicked, I want the script to verify a radio is checked, if one is checked to do nothing.

You won't be able to do this because you aren't ever actually getting your checked value with this line.

On a side note, I'll disagree with the blanket statement that you should check a radio button by default.

For example in a survey, you want the user to explicitly choose an answer. @Jon P - Radio button group represents a 1 of many selection like a single-select list or dropdown.

Its best to always have exactly one radio button selected.

This will eliminate the need for any Javascript and let the browser do the work for you.

Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

A friend recently asked for a code sample showing Java Script being used to client-side validate the radio inputs of an HTML form.

However, should you need to check for null or undefined, you should use typeof and strict comparison with === After reading your comment - I noticed one other thing.

Are you trying get get the value directly from Javascript by reading the DOM with like an onclick function?

Leave a Reply