jquery.validate file验证jpg图片的坑

之前写的验证:

file: {
                accept: "JPG|PNG"
            },

发现jpg图片一直验证不了,后来发现要这么写:

file: {
                accept: "JPEG|PNG"
            },

这个时候PNG,JPG和JPEG都是接受的,目前还不知道为什么,等待进一步的探索。。。
进一步的探索出来了:
我们看additional-methods.js里面关于accept的函数:

// Accept a value from a file input based on a required mimetype
$.validator.addMethod("accept", function(value, element, param) {
    // Split mime on commas in case we have multiple types we can accept
    var typeParam = typeof param === "string" ? param.replace(/\s/g, "").replace(/,/g, "|") : "image/*",
    optionalValue = this.optional(element),
    i, file;

    // Element is optional
    if (optionalValue) {
        return optionalValue;
    }

    if ($(element).attr("type") === "file") {
        // If we are using a wildcard, make it regex friendly
        typeParam = typeParam.replace(/\*/g, ".*");

        // Check if the element has a FileList before checking each file
        if (element.files && element.files.length) {
            for (i = 0; i < element.files.length; i++) {
                file = element.files[i];

                // Grab the mimetype from the loaded file, verify it matches
                if (!**file.type.match(new RegExp( "\\.?(" + typeParam + ")$", "i"))**) {
                    return false;
                }
            }
        }
    }

不难看出,里面判断文件是否符合要求是根据文件的type来进行判定的。而.jpg的文件类型又是什么呢?
.jpg图像的文件类型为JPEG
现在,jquery.validate file验证jpg的时候为什么要写“accept:JPEG”的问题就得到完美解决了。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值