Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fixes for radio button validation #488

Merged
merged 2 commits into from

2 participants

@bradrobertson

thanks for this!

I'm currently just working on bug fixes for the 1.2.6 release. It has proven difficult enough. We're hopefully releasing that very soon and I'll start working on feature requests.

Fiddle looks good though and much appreciated. Will definitely make merging this much simpler

@bradrobertson bradrobertson merged commit 87b61b1 into jquerytools:dev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Sep 7, 2011
  1. @lbarratt

    added radio button validation for single and grouped radios. made get…

    lbarratt authored
    …Position() use the first input element if a set of elements is available
  2. @lbarratt
This page is out of date. Refresh to see the latest.
Showing with 42 additions and 16 deletions.
  1. +42 −16 src/validator/validator.js
View
58 src/validator/validator.js
@@ -95,14 +95,17 @@
};
/* calculate error message position relative to the input */
- function getPosition(trigger, el, conf) {
+ function getPosition(trigger, el, conf) {
+
+ // Get the first element in the selector set
+ el = $(el).first() || el;
// get origin top/left position
- var top = trigger.offset().top,
- left = trigger.offset().left,
- pos = conf.position.split(/,?\s+/),
- y = pos[0],
- x = pos[1];
+ var top = trigger.offset().top,
+ left = trigger.offset().left,
+ pos = conf.position.split(/,?\s+/),
+ y = pos[0],
+ x = pos[1];
top -= el.outerHeight() - conf.offset[0];
left += trigger.outerWidth() + conf.offset[1];
@@ -256,12 +259,21 @@
return p.test(el.val());
});
+ v.fn(":radio", "Please select an option.", function(el) {
+ var checked = false;
+ var els = $("[name=" + el.attr("name") + "]").each(function(i, el) {
+ if ($(el).is(":checked")) {
+ checked = true;
+ }
+ });
+ return (checked) ? true : false;
+ });
function Validator(inputs, form, conf) {
// private variables
var self = this,
- fire = form.add(self);
+ fire = form.add(self);
// make sure there are input fields available
inputs = inputs.not(":button, :image, :reset, :submit");
@@ -388,6 +400,17 @@
els = els || inputs;
els = els.not(":disabled");
+
+ // filter duplicate elements by name
+ var names = {};
+ els = els.filter(function(){
+ var name = $(this).attr("name");
+ if (!names[name]) {
+ names[name] = true;
+ return $(this);
+ }
+ });
+
if (!els.length) { return true; }
e = e || $.Event();
@@ -401,7 +424,7 @@
var errs = [];
// loop trough the inputs
- els.not(":radio:not(:checked)").each(function() {
+ els.each(function() {
// field and it's error message container
var msgs = [],
@@ -545,22 +568,27 @@
});
}
- // checkboxes, selects and radios are checked separately
+ // checkboxes and selects are checked separately
inputs.filter(":checkbox, select").filter("[required]").bind("change.V", function(e) {
var el = $(this);
if (this.checked || (el.is("select") && $(this).val())) {
effects[conf.effect][1].call(self, el, e);
}
- });
-
- var radios = inputs.filter(":radio").change(function(e) {
- self.checkValidity(radios, e);
+ });
+
+ // get radio groups by name
+ inputs.filter(":radio[required]").bind("change.V", function(e) {
+ var els = $("[name=" + $(e.srcElement).attr("name") + "]");
+ if ((els != null) && (els.length != 0)) {
+ self.checkValidity(els, e);
+ }
});
// reposition tooltips when window is resized
$(window).resize(function() {
self.reflow();
});
+
}
@@ -593,6 +621,4 @@
};
-})(jQuery);
-
-
+})(jQuery);
Something went wrong with that request. Please try again.