代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>上传验证</title>
</head>
<body>
<div>
<label for="file">选择文件</label>
<input type="file" id="file" >
</div>
<script>
window.onload = function () {
var file = document.getElementById("file");
file.onchange = function () {
//获取上传图片的路径
var path = this.value;
//获取.在字符串中的位置
var location = path.lastIndexOf('.');
//截取后缀名
var suffix = path.substring(location);
// 统一转为小写
var small_suffix = suffix.toLowerCase(suffix);
if(small_suffix === '.jpg' || small_suffix === '.png' || small_suffix === '.jpeg'
|| small_suffix === '.gif'){
alert("上传格式正确!");
}else{
alert("上传格式有误!")
}
}
}
</script>
</body>
</html>
结果