js验证文件类型

2016-07-08

<textarea cols="50" rows="15" name="code" class="xhtml">&lt;%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%&gt; &lt;!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"&gt; &lt;HTML&gt; &lt;body&gt; &lt;form action="load.jsp" method="post" enctype="mutipart/form-data" name="myform"&gt;请选择上传文件: &lt;input TYPE="file" NAME="picture" /&gt; &lt;input TYPE="button" value="提 交" onclick="check(myform);" /&gt; &lt;input TYPE="button" value="取 消" onclick="window.close();" /&gt;&lt;/form&gt; &lt;/body&gt; &lt;/HTML&gt; &lt;mce:script language="javascript"&gt;&lt;!-- function isEmpty(str){ for(var i=0;i&lt;str.length;i++){ if(str.charAt(i)!==" "){ return false; } } return true; } function check(form){ var fileName = form.picture.value; if(isEmpty(fileName)){ alert("请选择文件名"); form.picture.focus(); return false; } //lastIndexOf如果没有搜索到则返回为-1 if(fileName.lastIndexOf(".")!=-1) { var fileType = (fileName.substring(fileName.lastIndexOf(".")+1,fileName.length)).toLowerCase(); var suppotFile = new Array(); suppotFile[0] = "gif"; suppotFile[1] = "bmp"; suppotFile[2] = "jpg"; for(var i =0;i&lt;suppotFile.length;i++){ if(suppotFile[i]==fileType){ return true; }else{ continue; } } alert("不支持文件类型"+fileType); return false; }else { alert("文件只支持JIF,BMP,JPG"); } } // --&gt;&lt;/mce:script&gt; </textarea>

转自http://blog.csdn.net/zsy_gemini/archive/2007/10/11/1820870.aspx