📌  相关文章
📜  如何在 javascript 代码示例中验证文件扩展名

📅  最后修改于: 2022-03-11 15:02:36.779000             🧑  作者: Mango

代码示例1
//If your code above is simply trying to get the file extension of the filename then you can split the filename to an array by . and then use pop(). You can also put the valid extensions in an array and use indexOf() to check for validity. Try this:

var validExt = [ 'pdf', 'jpg', 'png' ];
var ext = this.value.split('.').pop();

if (validExt.indexOf(ext.toLowerCase()) == -1) {
    alert('Not allowed file type');
    this.value = '';
}