jQuery - INPUT type=File ,图像文件类型验证选项? [英] jQuery - INPUT type=File , Image FileType Validation options?

查看:14
本文介绍了jQuery - INPUT type=File ,图像文件类型验证选项?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下几点:

<input id="user_profile_pic" name="user[profile_pic]" type="file">

在服务器上我检查以确保它是图像,但我想先检查客户端.

On the server I check to make sure it's an image, but I want to check on the clientside first.

如果选择的文件输入文件不是 gif、jpg、png 或 bmp,我如何使用 jQuery 提醒用户?

How with jQuery can I alert the user if the file input file selected isn't a gif,jpg,png, or bmp?

谢谢

推荐答案

你想检查元素的值是什么,类似于:

You want to check what the value of the element is, something along the lines of:

$("#user_profile_pic").change(function() {

    var val = $(this).val();

    switch(val.substring(val.lastIndexOf('.') + 1).toLowerCase()){
        case 'gif': case 'jpg': case 'png':
            alert("an image");
            break;
        default:
            $(this).val('');
            // error message here
            alert("not an image");
            break;
    }
});

编辑

根据注释更改代码 - 如果不是图像,现在会删除所选文件的值.

Code changed based on comment - now removes the value of the selected file if not an image.

这篇关于jQuery - INPUT type=File ,图像文件类型验证选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆