Skip to content

Instantly share code, notes, and snippets.

@ErfanEbrahimnia
Created January 23, 2013 10:44
Show Gist options
  • Save ErfanEbrahimnia/4604282 to your computer and use it in GitHub Desktop.
Save ErfanEbrahimnia/4604282 to your computer and use it in GitHub Desktop.
// 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>
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment