js如何检测上传图片的后缀格式

快乐打工仔 分类:实例代码

本章节分享一段代码实例,它实现了检测上传图片后缀格式的效果。

代码如下:

function Checkfiles(){
  var fup = document.getElementById('img');
  var fileName = fup.value;
  var ext = fileName.substring(fileName.lastIndexOf('.') + 1);
  if(ext == "gif" || ext == "GIF" || ext == "JPEG" || ext == "jpeg" || ext == "jpg" || ext == "JPG" || ext == "png" || ext == "PNG"){
    return true;
  }
  else{
    alert("Upload JPG, JPEG, PNG, GIF images only");
    fup.focus();
    return false;
  }
}

回复

我来回复
  • 暂无回复内容