Question

I've another button to replace the default file control so i can use a styled button and perform some additional actions.

Because i am using another button to upload files my client side validation method is not being called.

I'm currently doing this to validate my fields:

if ($(document).has('input[type="file"]')) {
window.setInterval(function () {
    $("#form").valid();
}, 1000);

}

This of course validates all the fields every second, i just want to validate my selected file.

My validator method:

$.validator.addMethod("fileextension", function (value, element, allowedextensions) {
var arrayAllowedExtensions = allowedextensions.split(',');
var fileExtension = value.split('.').pop();
$.each(arrayAllowedExtensions, function(extension) {
    if (extension == fileExtension) {
        return true;
    }
});
return false;

});

So my question is it possible to validate just one field? A snippet of code is appreciated a lot!

Thanks in advance!

Was it helpful?

Solution

You can call the valid() method on the result of a jquery selector, which could be used to restrict the validation to a single field.

You can write the following, based on the id attribute of your field:

$('#yourFieldId').valid();

You can also use the name attribute of the field:

$('input[name="yourFieldName"]').valid();

Or any other selector that restrict the field/fields you want to validate instead of the whole form.

Hope it helps!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top