gpt4 book ai didi

javascript - 检查 JavaScript 中的多个扩展名?

转载 作者:行者123 更新时间:2023-12-02 19:15:25 25 4
gpt4 key购买 nike

大家好,我如何使用 javascript 检查输入按钮中的多个文件扩展名,例如文件必须是 mp3、mp4

<p><input type="file" id="file" name="file" size="23"/></p><br />
<p><input type="submit" value="Upload file" /></p>

Javascript:

$(function () {
$('form').submit(function () {
var selectedFile = $('#file').val();
var matches = selectedFile.match(/\.(xlsx?)$/i);
if (matches == null) {
alert('please select an Excel file');
return false;
}
return true;
});
});

最佳答案

试试这个

var validFilesTypes = ["mp3", "mp4"];
function Validate(path)
{
var ext = path.substring(path.lastIndexOf(".") + 1, path.length).toLowerCase();
var isValidFile = false;
for (var i = 0; i < validFilesTypes.length; i++)
{
if (ext == validFilesTypes[i])
{
isValidFile = true;
break;
}
}
if (!isValidFile)
alert('Please select file of mp3 and mp4 extension only.');

return isValidFile;
}

关于javascript - 检查 JavaScript 中的多个扩展名?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13197314/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com