/ Published in: jQuery
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
// Javascript var $form = $('#form-id'), error; $form.submit(function() { error = 0; // check if radio groups are selected $form.find('input:radio').each(function() { var radioGroupName = $(this).attr('name'), $radioGroup = $form.find('input:radio[name='+radioGroupName+']'); // check only if radio group exists if( $radioGroup.size() ) { if( !$radioGroup.is(':checked') ) error = 1; } }); // handle error if(error) { // output your error message return false; } }); // HTML <form id="form-id" action="" method="post"> <input type="radio" name="my_radio_group" value="1" /> <input type="radio" name="my_radio_group" value="2" /> <input type="radio" name="my_radio_group" value="3" /> <input type="submit" value="Submit" /> </form>