Invoking custom jquery validator within another javascript function
How to invoke jQuery Custom Validator for a <input type='file' />
on onChange Event within normal javascript function. I need to capture the event and trigger a custom validator already registered, inside it creates a table with rows corresponding to each file attempted to upload.
I have read this article but I can't validate what the author is saying: Version of Custom Validator Calling
Thanks in advance.
I have a piece of jquery custom validator function that makes the magic, it checks valid files extensions (images are the main target right now) when uploading them to the server:
$(document).ready(function () {
$('#UploadedFile').live('change', function () {
//TODO
alert('Hey hey!');//It works, because the event is captured and calls this line
return jQuery.validator.methods.extensions.call(null, $(this), null);//Doesn't work for me
//This is for running validation in the client-side
});
});
jQuery.validator.addMethod("extensions", function (value, element, params) {
var extensionsAllowed = params.extensions;
var pattern = new RegExp(extensionsAllowed);
var elementToValidate = $(element);
var maxUploadSize = Math.floor(5000000 / 1024) / 1024;//converting to Kilobytes
var result = fnCreateUploadDetailsTable(elementToValidate, pattern, maxUploadSize);
return result;
//It works for server-side Data Annotations validator,
//but the request travels over the HTTP connection and I want to avoid overloading the server
//with this basic function
});
jQuery.validator.unobtrusive.adapters.add("extensions", ["extensions"], function (options) {
options.rules["extensions"] = options.params;
options.messages["extensions"] = options.message;
});
onfocusout
,onkeyup
,onclick
,onsubmit
, etc. Also, using theadditional-methods.js
file, you can assign the rule for file extensions... no need to create a new one. docs.jquery.com/Plugins/Validation/CustomMethods/… – Sparky Mar 7 '13 at 20:02