Validation rules applied to all elements
jquery, jquery-validate, unobtrusive-validation
Solution
Your code as you posted cannot do what you claim. Provide a concise working demo to show otherwise.
See this demo where the `rule` is only applied to the specified element.
http://jsfiddle.net/tyCvL/
jQuery:
$(document).ready(function () {
$('#myform').validate({ // initialize the plugin
// options
});
$("#field1").rules("add", { required: true });
});
HTML:
<form id="myform">
<input type="text" name="field1" id="field1"/>
<input type="text" name="field2" id="field2"/>
</form>
EDIT:
As per example above, the `name` attribute is not optional. The jQuery Validate plugin will always require that a `name` attribute be present on all `input` elements for all situations and cases.
OP's jsFiddle Fixed: http://jsfiddle.net/emZB8/5/
<form id="myform">
<input type="text" name="field1" id="field1"/>
<input type="text" name="field2" id="field2"/>
</form>
Problem
I need to assign validation rules to specific input elements by `id` not `name`. To do this I'm executing the following code: ``` $("#cvc").rules("add", { required: true, validateCvc: true }); $("#cardNumber").rules("add", { creditcard: true, messages: { creditcard: "Invalid Card Number" } }); ``` However, this is actually assigning the rules to all inputs on the form. I can verify this by logging the rules attached to a different input: ``` console.log($("#name").rules()); ``` Output: ``` Object {required: true, validateCvc: true, creditcard: true} ``` Update It looks like this may have something to do with the jQuery Validate Unobtrusive plugin which I'm beginning to find is more hassle than it's worth. The code that sets up the validation is calling `validate` before I add the rules: ``` function validationInfo(form) { var $form = $(form), result = $form.data(data_validation), onResetProxy = $.proxy(onReset, form); if (!result) { result = { options: { // options structure passed to jQuery Validate's validate() method errorClass: "input-validation-error", errorElement: "span", errorPlacement: $.proxy(onError, form), invalidHandler: $.proxy(onErrors, form), messages: {}, rules: {}, success: $.proxy(onSuccess, form) }, attachValidation: function () { $form .unbind("reset." + data_validation, onResetProxy) .bind("reset." + data_validation, onResetProxy) .validate(this.options); }, validate: function () { // a validation function that is called by unobtrusive Ajax $form.validate(); return $form.valid(); } }; $form.data(data_validation, result); } return result; } ``` Update 2 Here's a repo - http://jsfiddle.net/benfosterdev/emZB8/ You can see in the console that this is clearly setting up a "required" rule for both fields even though it was not specified on the second. It would appear if you want to work with validate plugin directly you're better off removing the reference to the unobtrusive plugin.