I have two bits of code: a form and a jQuery function.
My problem is that I would like to get class attributes and use them on the form validation function. I am using Validity JS for validation.
This is what I have:
<input type="text" name="name" value="" id="field_01" class=".require().minLength(5)" />
and in the JS function:
var checks_to_apply = $("#field_01").attr("class").split(" "),
i, l;
var checks = "";
for (i = 0, l = checks_to_apply.length; i < l; i++) {
checks += checks_to_apply[i];
}
$("#field_01")+checks;
...
...
The right behaviour here is not respected.
This is what I get from a “checks” alert:
.require().minLength(5)
but executing in that way it is not the same as doing it directly in the jQuery function like:
$("#field_01").require().minLength(5);
Where am I wrong, I think that the problem in the method may be because it is seeing .require().minLength(5) as a string and not as jQuery code to execute.
Any idea on how to fix it, please?
as you are executing these functions on a particular object you would need to create the whole argument as an string and execute it via eval():
But i would advise you to use a different validation plugin, because you are misusing the class attribute: Check out http://jquery.bassistance.de/validate/demo/ for a much better and cleaner annotation.