js 上传文件后缀名的判断 var flag=false;应用
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>用户注册——凤凰网</title>
<script type="text/javascript">
//3、(字符)检查文件上传表单控件,如果含有[jpg,jpeg,gif,png]则显示“文件类型合法”,否则“显示文件类型错误” function checkFileExt(filename)
{
var flag = false; //状态
var arr = ["jpg","png","gif"];
//取出上传文件的扩展名
var index = filename.lastIndexOf(".");
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>
</head> <body>
<form name="form1" method="post" enctype="multipart/form-data">
上传头像:<input type="file" name="uploadFile" onchange="checkFileExt(this.value)" />
</form>
<input type="button" value="返回上一页" onclick="javascript:history.go(-1)" />
</body>
</html>