Return to Snippet

Revision: 45365
at April 28, 2011 22:21 by jurajhilje


Updated Code
// 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>

Revision: 45364
at April 28, 2011 22:17 by jurajhilje


Initial Code
// 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" />
</form>

Initial URL

                                

Initial Description

                                

Initial Title
jQuery radio input validation

Initial Tags
jquery, validation

Initial Language
jQuery