Validation rules applied to all elements
I need to assign validation rules to certain input elements to id
not name
.
To do this, I execute the following code:
$("#cvc").rules("add", { required: true, validateCvc: true });
$("#cardNumber").rules("add",
{ creditcard: true, messages: { creditcard: "Invalid Card Number" } });
However, this actually assigns rules to all inputs on the form. I can test this by registering the rules attached to another input:
console.log($("#name").rules());
Output:
Object {required: true, validateCvc: true, creditcard: true}
Update
It looks like it might have something to do with the jQuery Validate Unobtrusive plugin I'm starting to look for, it's more hassle than it's worth. The code that sets up the check calls validate
before adding 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 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 the repo - http://jsfiddle.net/benfosterdev/emZB8/
You can see in the console that this explicitly sets up a "required" rule for both fields, even if it is not listed on the second.
It would seem that if you want to work with the validate plugin directly, you'd better remove the link to the unobtrusive plugin.
source to share
Your code you posted cannot do what you claim. Provide a short working demo to show otherwise.
See this demo where rule
it only applies to the specified item.
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 shown above, the attribute name
is not optional. The jQuery Validate plugin always requires an attribute name
on all elements input
for all situations and cases.
OP 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>
source to share
I had a similar problem where a validation meant for one field would apply to all other fields. After setting "debug" to true, in jquery validate started throwing "field" has no name assigned "in the console. I set the name attribute on these problem fields and voila .. solved the problem.
So, I think jquery validation doesn't like unnamed fields.
source to share