Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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 $others = $form.find('input[name="'+$(this).attr('name')+'"]');
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rename $others since it includes itself

$others.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