Home > Web Front-end > JS Tutorial > js determines whether the uploaded file suffix is ​​legal_javascript skills

js determines whether the uploaded file suffix is ​​legal_javascript skills

WBOY
Release: 2016-05-16 15:17:27
Original
1340 people have browsed it

The example in this article introduces the detailed code of js to determine whether the suffix name of the uploaded file is legal. It is shared with everyone for your reference. The specific content is as follows

Rendering:

Select file

Select 1.jpg file

Upload successful and legal

Except for image file formats, all other operations are illegal.

Specific code:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<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>
Copy after login

I hope this article will be helpful to everyone learning JavaScript programming.

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template