我有一些javascript在上传之前验证图像名称。它忽略了文件路径,并在IE和Chrome上正常工作,但无论在firefox或safari中使用此功能,我都会收到不正确的文件警报。
function validate(elem){
var alphaExp = /^(?:[w]:||\)(\[a-zA-Z_0-9-.]+)+.(jpg|jpeg|pjpeg|bmp|png|pdf|doc|docx|pub|pubx|id|psd|ai|eps|gif|tiff|zip|rar)$/;
if(elem.value.match(alphaExp)){
return true;
} else{
alert("File name or type is not suitable! nnPlease ensure the file type is one we accept and is named without spaces or special characters.");
elem.focus();
return false;
}
}
使用change事件触发Firefox和Safari中的validate
函数:
document.getElementById("fileUpload").addEventListener("change", validate);
引用
- 方法:输入类型=文件对象
- type属性的状态:File Upload state (type= File)
- 使用input type="file"上传文件字段与。change()事件并不总是触发在IE和Chrome
- Jquery:将事件更改为IE 中的输入文件