I've got an image upload that is created on a dynamic form. I am trying to make it so that I can validate that the file uploaded is an actual image, and do this unobtrusively. I've read about the .rules() settings and using the accept property, but that requires you to input a field name (http://docs.jquery.com/Plugins/Validation/Methods/accept). Since the forms are dynamic I won't know the field name. My view that generates the upload input looks like this:
<input type="file" size="20" name="@(Model.FormItem.Id + "-image")" id="@(Model.FormItem.Id + "-image")" />
So all my file inputs have an "-image" suffix. Is there a way to add a rule using a jquery selector for the validator rules instead of using a specific field name??
EDIT Alright it's clear that I am not explaining what the problem is well. Perhaps the solution is so simple and somehow I just missed it. I know how to use jQuery selectors, but from the examples I have seen of the rules syntax you have to supply a static field name for the rules. How can I assign validate rules to a selector instead of a static field name??
So right now this is what I have :
var validatorData = $('#items-form').data('validator');
$.extend(validatorData.settings, {
rules: {
static_field_name: { accept: "jpg|png" }
},
messages: {
static_field_name_message: "The file must be an image."
}
});
Which won't work, nor does (obviously) if I replace static_field_name with the proper selector $('input[name$="-image"]'). So how do I do this?
nameattribute is generated since you have that-imagesuffix, you can probably get them like$(input[name$="-image"])