验证文件格式是否正确
html部分
<input type="file" name="" id="file"/>
js部分
var file=document.getElementById("file");
file.onchange=function(){
var path=this.value; //得到的是现在的文件路径
//alert(path);
var suxxif=path.substr(path.lastIndexOf(".")).toUpperCase(); //得到的是后缀名 toUpperCase()用于将字符串转化为大写
if (suxxif==".PNG"||suxxif==".PNG"){
this.nextSibling.innerHTML("格式是正确的");
}else{
this.nextSibling.innerHTML("格式是错误的");
}
//alert(suxxif); 能够获取相应的后缀名称
//分析:需要获取.的位置,判断点的后4位是否正确
}