我想过滤文件上传。它在FireFox中工作正常,但在谷歌浏览器中它始终显示无效文件,尽管文件有效。正则表达式不适用于谷歌浏览器
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.
任何原因,你正在做一个区分大小写的考验吗?您在开始处包含'a-zA-Z',但某些文件扩展名只允许小写。如果你给正则表达式添加一个'i'标志,它是否解决了这个问题?如:'var reg =/yourregexhere/i;'(这也将简化模式,因为您不需要同时指定'.jpg'和'.JPG'等) – nnnnnn
此正则表达式的哪个文件名失败?你能给个例子吗? –
文件扩展名之前的点'.'需要被转义,并且除'']之外的那个字符类中的所有字符不需要被转义。 – Bergi