EasyUI Forum

General Category => EasyUI for jQuery => Topic started by: rumbleDom on January 10, 2013, 09:45:21 AM



Title: required radiobuttons in a form
Post by: rumbleDom on January 10, 2013, 09:45:21 AM
Hello ...
if I need a radio from a user in a form, how can I do that?
This code is not working :-(
Code:
<input type="Radio" name="Gender" value="1" checked="checked" class="easyui-validatebox" required="true">


Title: Re: required radiobuttons in a form
Post by: stworthy on January 10, 2013, 06:31:21 PM
If you want to validate if a field is selected. Use the checkbox field instead. The validation on a checkbox field is not supported directly but extending a new validator is possible.
Code:
$.extend($.fn.validatebox.defaults.rules, {  
checked: { 
validator: function(value, param){ 
var input = $(param[0]);
input.unbind('.cc').bind('click.cc',function(){
$(this).focus();
});
return input.is(':checked'); 
}, 
message: 'Please check this field.' 

});
Code:
<input id="gg" type="checkbox" name="Gender" value="1" checked="checked" class="easyui-validatebox" validType="checked['#gg']">


Title: Re: required radiobuttons in a form
Post by: chrwei on September 18, 2013, 08:00:16 AM
I have made a validator for radio buttons.

Code:
	$.extend($.fn.validatebox.defaults.rules, {
requireRadio: { 
validator: function(value, param){ 
var input = $(param[0]);
input.off('.requireRadio').on('click.requireRadio',function(){
$(this).focus();
});
return $(param[0] + ':checked').val() != undefined;
}, 
message: 'Please choose option for {1}.' 

});

it used by adding it to only the first radio in a group.  adding to all is no harm, it just wastes CPU.
Code:
<input class="easyui-validatebox" type="radio" name="yes_no" value="1" data-options="validType:'requireRadio[\'#ff input[name=yes_no]\', \'Yes or no\']'">Yes

<input class="easyui-validatebox" type="radio" name="yes_no" value="0">No