Skip to content

Commit 393ccab

Browse files
haackedjzaefferer
authored andcommitted
Fixed issue 78. Error/Valid styling applies to all radio buttons of same group for required validation.
1 parent 3a42224 commit 393ccab

File tree

3 files changed

+34
-4
lines changed

3 files changed

+34
-4
lines changed

jquery.validate.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,11 +238,19 @@ $.extend($.validator, {
238238
else if (element.parentNode.name in this.submitted)
239239
this.element(element.parentNode);
240240
},
241-
highlight: function( element, errorClass, validClass ) {
242-
$(element).addClass(errorClass).removeClass(validClass);
241+
highlight: function(element, errorClass, validClass) {
242+
if (element.type === 'radio') {
243+
this.findByName(element.name).addClass(errorClass).removeClass(validClass);
244+
} else {
245+
$(element).addClass(errorClass).removeClass(validClass);
246+
}
243247
},
244-
unhighlight: function( element, errorClass, validClass ) {
245-
$(element).removeClass(errorClass).addClass(validClass);
248+
unhighlight: function(element, errorClass, validClass) {
249+
if (element.type === 'radio') {
250+
this.findByName(element.name).removeClass(errorClass).addClass(validClass);
251+
} else {
252+
$(element).removeClass(errorClass).addClass(validClass);
253+
}
246254
}
247255
},
248256

test/index.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,11 @@ <h2 id="qunit-userAgent"></h2>
139139
<input id="testEmail9" class="{required:true,email:true,messages:{required:'required',email:'email'}}" />
140140
</form>
141141

142+
<form id="testForm10">
143+
<input type="radio" name="testForm10Radio" value="1" id="testForm10Radio1" />
144+
<input type="radio" name="testForm10Radio" value="2" id="testForm10Radio2" />
145+
</form>
146+
142147
<div id="simplecontainer">
143148
<h3></h3>
144149
</div>

test/test.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,23 @@ test("form(): checkboxes: min/required", function() {
124124
$('#form6check2').attr("checked", true);
125125
ok( v.form(), 'Valid form' );
126126
});
127+
128+
test("form(): radio buttons: required", function () {
129+
expect( 6 );
130+
var form = $('#testForm10')[0];
131+
132+
var v = $(form).validate({ rules: { testForm10Radio: "required"} });
133+
ok(!v.form(), 'Invalid Form');
134+
equals($('#testForm10Radio1').attr('class'), 'error');
135+
equals($('#testForm10Radio2').attr('class'), 'error');
136+
137+
$('#testForm10Radio2').attr("checked", true);
138+
ok(v.form(), 'Valid form');
139+
140+
equals($('#testForm10Radio1').attr('class'), 'valid');
141+
equals($('#testForm10Radio2').attr('class'), 'valid');
142+
});
143+
127144
test("form(): selects: min/required", function() {
128145
expect( 3 );
129146
var form = $('#testForm7')[0];

0 commit comments

Comments
 (0)