I am trying to get get a group of inputs to validate as one. So if any of the inputs is empty, it will show a message below the inputs. I have been following another SO answer here. The 4 inputs will not validate. They just don't do anything when there is no data on submit. My other inputs validate just fine. Here is my form:
<div class="creditcard form-group">
<h2>Credit Card</h2>
@Html.TextBoxFor(model => model.cardNumber, new { @class = "form-control cc", @id = "CardNum", @placeholder = "Card Number" })
@Html.TextBoxFor(model => model.name, new { @class = "form-control cap cc", @id = "FullName", @placeholder = "Full Name" })
@Html.TextBoxFor(model => model.expDate, new { @class = "form-control cc", @id = "CardExp", @placeholder = "MM/YY" })
@Html.TextBoxFor(model => model.cvv, new { @class = "form-control cc", @id = "CardCVV", @placeholder = "CVV" })
@Html.HiddenFor(model => model.ccType)
....
And my jquery that is validating:
$.validator.addMethod("require_from_group", function (value, element, options) {
var valid = $(options[1], element.form).filter(function () {
return $(this).val();
}).length >= options[0];
return valid;
}, $.validator.format("Please fill out at least {0} of these fields."));
$("form").validate({
rules: {
cardNumber: { require_from_group: [2, ".cc"] },
name: { require_from_group: [2, ".cc"] },
expDate: { require_from_group: [2, ".cc"] },
cvv: { require_from_group: [2, ".cc"] }
}
});
This is MVC 5 if that helps.