在另一个 javascript 函数中调用自定义 jquery 验证器
如何<input type='file' />
在普通 javascript 函数中为 onChange 事件调用 jQuery 自定义验证器。我需要捕获事件并触发已注册的自定义验证器,在它内部创建一个表,其中包含与尝试上传的每个文件相对应的行。我已经阅读了这篇文章,但我无法验证作者所说的内容:自定义验证器调用的版本
提前致谢。
我有一个 jquery 自定义验证器函数,它可以在将它们上传到服务器时检查有效的文件扩展名(图像是现在的主要目标):
$(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;
});