0

我正在尝试使用javascript验证用户在客户端网页的FileUpload控件中选择的图像文件。谁能告诉我我该怎么做。我正在尝试这个代码不起作用:

function validateFileExtension() 
{
    var re =/\.(gif|jpg|JPEG|tiff|png)$/i;
    var temp = document.getElementById('<%=FileUpload1.ClientID %>').value;

    if (re.test(temp)) 
    {
        return ("Invalid image file type.");

        return false;
    }

    return "";
}
4

2 回答 2

4

检查这对我有用...

    function validateImage() {
        var uploadcontrol = document.getElementById('<%=imgUploader.ClientID%>').value;
        //Regular Expression for fileupload control.
        var reg = /^(([a-zA-Z]:)|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg|.jpeg|.png|.gif)$/;
        if (uploadcontrol.length > 0) {
            //Checks with the control value.
            if (reg.test(uploadcontrol)) {
                return true;
            }
            else {
                //If the condition not satisfied shows error message.
                alert("Only .jpg, .jpeg,.png, .gif  files are allowed!");
                return false;
            }
        }
    } //End of function validate.

于 2013-03-09T16:03:33.547 回答
0
private  final String IMAGE_PATTERN = "([^\\s]+(\\.(?i)(jpg|png|gif|bmp))$)";
于 2013-08-05T16:55:18.343 回答