我想過濾文件上傳。它在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