Skip to content

add support for radio button validation #4

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
zipCode: function(zip) {
var re = /^\d{5}(-?\d{4})?$/;
return re.test(zip) ? true : "Invalid zip code (ex. 12345 or 12345-1234)";
},
oneChecked: function(check) {
return $(this).closest('form').find('input[name="'+$(this).attr('name')+'"]:checked').length === 1;
}
}
};
Expand All @@ -35,6 +38,10 @@
margin-bottom: 16px;
}

input[type="radio"] {
display: inline;
}

ol {
list-style-type: none;
}
Expand Down Expand Up @@ -72,6 +79,11 @@
<label for="zip">Zip</label>
<input type="text" id="zip" data-validate="zipCode" />
</li>
<li>
<label for="address">Address</label>
<input type="radio" name="address" value="home" data-validate="oneChecked">Home
<input type="radio" name="address" value="work" data-validate="oneChecked">Work
</li>
<li>
<input type="submit" />
</li>
Expand Down
13 changes: 13 additions & 0 deletions tinyvalidation.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,23 @@
}
}
};
var validateAllFieldsWithName = function (e) {
var $radios = $form.find('input[name="'+$(this).attr('name')+'"]');
$radios.each(function () {
if (!$(this).data('validate')) return;
$(this).trigger({
type: "tv-radioPropagate"
});
});
};

if (options.validateOnBlur) $(this).blur(validateField);
if (options.validateOnKeyUp) $(this).keyup(validateField);
if ($(this).is(':checkbox')) $(this).change(validateField);
if ($(this).is(':radio')) {
$(this).change(validateAllFieldsWithName);
$(this).bind('tv-radioPropagate', validateField);
}
if (options.disableSubmit) {
$(this).bind('input', validateField);
$(this).trigger('input');
Expand Down