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

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

问题描述

我有以下内容:

<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,图像FileType验证选项?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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