Skip to content

Commit c815f23

Browse files
committed
added radio button validation for single and grouped radios. made getPosition() use the first input element if a set of elements is available
1 parent 29a0e14 commit c815f23

File tree

1 file changed

+31
-16
lines changed

1 file changed

+31
-16
lines changed

src/validator/validator.js

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -95,14 +95,17 @@
9595
};
9696

9797
/* calculate error message position relative to the input */
98-
function getPosition(trigger, el, conf) {
98+
function getPosition(trigger, el, conf) {
99+
100+
// Get the first element in the selector set
101+
el = $(el).first() || el;
99102

100103
// get origin top/left position
101-
var top = trigger.offset().top,
102-
left = trigger.offset().left,
103-
pos = conf.position.split(/,?\s+/),
104-
y = pos[0],
105-
x = pos[1];
104+
var top = trigger.offset().top,
105+
left = trigger.offset().left,
106+
pos = conf.position.split(/,?\s+/),
107+
y = pos[0],
108+
x = pos[1];
106109

107110
top -= el.outerHeight() - conf.offset[0];
108111
left += trigger.outerWidth() + conf.offset[1];
@@ -256,12 +259,21 @@
256259
return p.test(el.val());
257260
});
258261

262+
v.fn(":radio", "Please select an option.", function(el) {
263+
var checked = false;
264+
var els = $("[name=" + el.attr("name") + "]").each(function(i, el) {
265+
if ($(el).is(":checked")) {
266+
checked = true;
267+
}
268+
});
269+
return (checked) ? true : false;
270+
});
259271

260272
function Validator(inputs, form, conf) {
261273

262274
// private variables
263275
var self = this,
264-
fire = form.add(self);
276+
fire = form.add(self);
265277

266278
// make sure there are input fields available
267279
inputs = inputs.not(":button, :image, :reset, :submit");
@@ -401,7 +413,7 @@
401413
var errs = [];
402414

403415
// loop trough the inputs
404-
els.not(":radio:not(:checked)").each(function() {
416+
els.not(":radio:checked").each(function() {
405417

406418
// field and it's error message container
407419
var msgs = [],
@@ -545,22 +557,27 @@
545557
});
546558
}
547559

548-
// checkboxes, selects and radios are checked separately
560+
// checkboxes and selects are checked separately
549561
inputs.filter(":checkbox, select").filter("[required]").bind("change.V", function(e) {
550562
var el = $(this);
551563
if (this.checked || (el.is("select") && $(this).val())) {
552564
effects[conf.effect][1].call(self, el, e);
553565
}
554-
});
555-
556-
var radios = inputs.filter(":radio").change(function(e) {
557-
self.checkValidity(radios, e);
566+
});
567+
568+
// get radio groups by name
569+
inputs.filter(":radio[required]").bind("change.V", function(e) {
570+
var els = $("[name=" + $(e.srcElement).attr("name") + "]");
571+
if ((els != null) && (els.length != 0)) {
572+
self.checkValidity(els, e);
573+
}
558574
});
559575

560576
// reposition tooltips when window is resized
561577
$(window).resize(function() {
562578
self.reflow();
563579
});
580+
564581
}
565582

566583

@@ -593,6 +610,4 @@
593610

594611
};
595612

596-
})(jQuery);
597-
598-
613+
})(jQuery);

0 commit comments

Comments
 (0)