【问题标题】:Image dimension rules using jQuery validation plugin [duplicate]使用 jQuery 验证插件的图像尺寸规则 [重复]
【发布时间】:2016-11-15 12:47:14
【问题描述】:

是否可以使用 jQuery 验证插件指定图像尺寸规则?例如,假设我希望上传的图片宽度至少为 600 像素。是否可以为此设置规则?如果有,如何实现?

【问题讨论】:

    标签: javascript jquery jquery-validate


    【解决方案1】:

    您可以在提交表单之前检查它们:

    window.URL = window.URL || window.webkitURL;
    
        $("form").submit( function( e ) {
            var form = this;
            e.preventDefault(); //Stop the submit for now
                                        //Replace with your selector to find the file input in your form
            var fileInput = $(this).find("input[type=file]")[0],
                file = fileInput.files && fileInput.files[0];
    
            if( file ) {
                var img = new Image();
    
                img.src = window.URL.createObjectURL( file );
    
                img.onload = function() {
                    var width = img.naturalWidth,
                        height = img.naturalHeight;
    
                    window.URL.revokeObjectURL( img.src );
    
                    if( width == 400 && height == 300 ) {
                        form.submit();
                    }
                    else {
                        //fail
                    }
                };
            }
            else { //No file was input or browser doesn't support client side reading
                form.submit();
            }
    
        });
    

    【讨论】:

    • 如果没有文件或者浏览器不支持客户端读取,为什么要提交表单?
    猜你喜欢
    • 2021-07-03
    • 2015-04-14
    • 2018-09-22
    • 1970-01-01
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多