0

During the submission of my form, I need to validate my radio buttons to see if they were selected.

My page layout asks for the education level in multiple fields like so:

enter image description here

My code for the options look like so:

<input type="radio" name="proficiency[journalism]" value="4yr">
<input type="radio" name="proficiency[journalism]" value="working">
<input type="radio" name="proficiency[journalism]" value="some">
<input type="radio" name="proficiency[journalism]" value="none">

I need to be able to loop through name="proficiency[journalism]" and say if none of them were selected, throw an error.

I just need to make sure at least one has been selected.

How can I accomplish this?

SBB
  • 8,560
  • 30
  • 108
  • 223

1 Answers1

0

Try this:

if ( $('input:checked[name="proficiency[journalism]"]').length > 0 ) {
    // success
} else {
    // error
}

The conditional verifies that there is at least one checked input of that name (you can only select one since it is a radio type), and performs the else function if nothing was selected.

PMG
  • 190
  • 1
  • 8
  • Can a selector have an array in it? Error: Syntax error, unrecognized expression: input:checked[name="proficiency[journalism]" – SBB Dec 24 '13 at 18:13
  • It's not an array, it's the string value of the "name" attribute. http://jsfiddle.net/Paul_Geronca/5JX4V/ – PMG Dec 24 '13 at 18:43