1

我想过滤文件上传器。它在 FireFox 中运行良好,但在 Google Chrome 中它总是显示无效文件,尽管文件是有效的。

function validate() {
    var uploadcontrol = document.getElementById('<%=fileupload.ClientID%>').value;

    //Regular Expression for fileupload control.
    //var reg = /^(([a-zA-Z])|(\\{2}\w+)\$?)(\\(\w[\w].*))+(.jpg|.jpeg|.jpe|.gif|.bmp|.png|.JPG|.JPEG|.JPE|.GIF|.BMP|.PNG)$/;
    var reg = /^(([0-9a-zA-Z\^\&\'\@\{\}\[\]\,\$\=\!\-\#\(\)\%\+\~\_ ]))+(.jpg|.jpeg|.jpe|.gif|.bmp|.png|.JPG|.JPEG|.JPE|.GIF|.BMP|.PNG|.dds|.psd|.pspimage|.tga|.thm|.tif|.tiff|.yuv)$/;

    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 Images are allowed!");
            return false;
        }
    }
} //End of function validate.
4

2 回答 2

1

这是一个非常奇怪的正则表达式。试试这个

var reg = /^[^\\//]+\.(jpg|jpeg|jpe|gif|bmp|png|dds|psd|pspimage|tga|thm|tif|tiff|yuv)$/i;

这个适用于我在 chrome 中使用各种文件名。

最后的 i 修饰符会忽略大小写,这样名字匹配就大方一点。

于 2013-05-17T12:04:26.190 回答
0

您的测试似乎确实在 chrome 中运行,请参阅 this fiddle

因此,我认为您需要调试uploadcontrolchrome 和 firefox 中的值以查看它们的不同之处,然后调整您的正则表达式。

于 2013-05-17T12:07:25.553 回答