<script type="text/javascript" charset="utf-8"> function checkFile(filename){ var flag = false; var arr = ["jpg","png","gif","bmp"]; //取扩展名; var index = filename.indexOf("."); var ext = filename.substr(index+1); for(var i = 0; i<arr.length; i++){ if(ext==arr[i]){ flag = true; break; } } if(flag){ document.write("合法"); }else{ document.write("不合法"); } } </script>
<form name="form" method="post" enctype="multipart/form-data"> 上传图片:<input id="button" type="file" name="uploadType" onchange="checkFile(this.value)"/> </form>