Skip to content
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
4 changes: 2 additions & 2 deletions build/example.htm
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@



<script src="jquery-1.8.0.min.js"></script>
<script src="jquery-1.10.2.min.js"></script>
<script src="jquery.validity.js"></script>
<script>
$("form").validity(function() {
$("#requiredInput").require();
});
</script>
</body>
</html>
</html>
17 changes: 17 additions & 0 deletions src/jquery.validity.core.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ $.validity = {
messages:{

require:"#{field} is required.",
requireOne:"At least one field is required.",

// Format validators:
match:"#{field} is in an invalid format.",
Expand Down Expand Up @@ -324,6 +325,22 @@ $.fn.extend({
);
},

// At least one of these fields should be filled
requireOne:function(msg) {
var valid = validate(
this,
(function() {
var group = this;
// Uses serializeArray to filter out unchecked checkboxes
return !!($(group).serializeArray().filter(function(e) { return e.value }).length);
}).bind(this),
// Uses bind to ignore validate(), we don't want validation errors
// when some inputs are not filled, only when none are.
msg || $.validity.messages.requireOne
);
return valid.length ? this : [];
},

// Validate whether the field matches a regex.
match:function(rule, msg) {

Expand Down
2 changes: 1 addition & 1 deletion src/jquery.validity.css
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ label.error {
* an extra bit of text (or anything really) that explains what the summary is.
* '.validity-erroneous' is applied to every input that fails.
*/
.validity-summary-container { display:none; }
.validity-summary-container { }
.validity-summary-output ul { }
.validity-erroneous { border:solid 2px #f56600 !important; }

Expand Down
1 change: 1 addition & 0 deletions tests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<script src="unit-tests/common/nonHtml.js"></script>
<script src="unit-tests/common/range.js"></script>
<script src="unit-tests/common/require.js"></script>
<script src="unit-tests/common/requireOne.js"></script>

<script src="unit-tests/common/match/date.js"></script>
<script src="unit-tests/common/match/email.js"></script>
Expand Down
27 changes: 27 additions & 0 deletions tests/unit-tests/common/requireOne.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module("common", { setup:setup8Inputs });

test("$.fn.requireOne()", 3, function() {
var expected, result;

$('#qunit-fixture input:odd').val('a value');
$('#qunit-fixture input:even').val('');
$.validity.start();
$('#qunit-fixture input').requireOne();
result = $.validity.end().errors;
expected = 0;
equal(result, expected, "require validation passes when some imputs are filled");

$('#qunit-fixture input').val('');
$.validity.start();
$('#qunit-fixture input').require();
result = $.validity.end().errors;
expected = 8;
equal(result, expected, "require validation fails on all inputs when all are empty");

$('#qunit-fixture input').val('value');
$.validity.start();
$('#qunit-fixture input').require();
result = $.validity.end().errors;
expected = 0;
equal(result, expected, "require validation does not fail when there are not empty inputs");
});