js验证文件格式return false不能停止运行,button依然提交 10
<script>functiongetPhotoSize(obj){photoExt=obj.value.substr(obj.value.lastIndexOf("."...
<script>
function getPhotoSize(obj){
photoExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();//获得文件后缀名
if(photoExt!='.jpg' && photoExt!='.png' && photoExt!='.jpeg' && photoExt!='.gif'){
alert("请上传后缀名为jpg,png,jpeg,gif的照片!");
return false;
}
var fileSize = 0;
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
if (isIE && !obj.files) {
var filePath = obj.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
}else {
fileSize = obj.files[0].size;
}
fileSize=Math.round(fileSize/1024*100)/100; //单位为KB
if(fileSize>=10){
alert("照片最大尺寸为10KB,请重新上传!");
return false;
}
}
</script>
<input type="file" id="imgFile" name="upload" onchange="getPhotoSize(this)"/>
<input type="submit" name="button" id="button" onclick="return getPhotoSize(obj)"value="提交" /> 展开
function getPhotoSize(obj){
photoExt=obj.value.substr(obj.value.lastIndexOf(".")).toLowerCase();//获得文件后缀名
if(photoExt!='.jpg' && photoExt!='.png' && photoExt!='.jpeg' && photoExt!='.gif'){
alert("请上传后缀名为jpg,png,jpeg,gif的照片!");
return false;
}
var fileSize = 0;
var isIE = /msie/i.test(navigator.userAgent) && !window.opera;
if (isIE && !obj.files) {
var filePath = obj.value;
var fileSystem = new ActiveXObject("Scripting.FileSystemObject");
var file = fileSystem.GetFile (filePath);
fileSize = file.Size;
}else {
fileSize = obj.files[0].size;
}
fileSize=Math.round(fileSize/1024*100)/100; //单位为KB
if(fileSize>=10){
alert("照片最大尺寸为10KB,请重新上传!");
return false;
}
}
</script>
<input type="file" id="imgFile" name="upload" onchange="getPhotoSize(this)"/>
<input type="submit" name="button" id="button" onclick="return getPhotoSize(obj)"value="提交" /> 展开
1个回答
2015-08-19
展开全部
<input type="submit" name="button" id="button" onsubmit="return getPhotoSize(obj)" value="提交" />
追问
还是不行 ,依然能提交啊
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询