图像尺寸最大尺寸jQuery插件

时间:2016-08-26 21:32:38

标签: javascript jquery jquery-validate

我正在使用jQuery验证插件来验证图像。我有一个验证要求,其中图像必须大于250像素×250像素,如果图像不是那么插件将引发错误。这是最小图像尺寸函数的代码:

$.validator.addMethod('minImageSize', function(value, element, minSize) {
          var imageSize = $(element).data('imageSize');
          return (imageSize) && (imageSize.width >= minSize.width) && (imageSize.height >= minSize.height);
        }, function(minSize, element) {
          return ($(element).data('imageSize')) ? ("Your image's size must be at least " + minSize.width + "px by " + minSize.height + "px") : "Selected file is not an image.";
        });

有没有办法创建maxSize函数?如果用户上传的图像大于指定的尺寸,它会抛出错误?让我们说最大图像大小是6000像素×6000像素,如果用户上传的内容大于它会引发错误。

1 个答案:

答案 0 :(得分:0)

如果您的minImageSize方法有效,那么修改以创建minImageSize方法似乎微不足道......

$.validator.addMethod('maxImageSize', function(value, element, maxSize) {
    var imageSize = $(element).data('imageSize');
    return (imageSize) && (imageSize.width < maxSize.width) && (imageSize.height < maxSize.height);
    }, function(maxSize, element) {
        return ($(element).data('imageSize')) ? ("Your image's size must be less than " + maxSize.width + "px by " + maxSize.height + "px") : "Selected file is not an image.";
    }
);