Mr. Mak
Mr. Mak

Reputation: 867

How to filter file input by file name

Can I use filter for <input type="file" accept=""/> with file name prefix/suffix ? For example for '_32' suffix will filter files from Fig-A to Fig-B.

Fig-A enter image description here

Fig-B enter image description here

Upvotes: 1

Views: 2151

Answers (1)

Quentin
Quentin

Reputation: 944013

No.

The accept attribute only accepts MIME types and file extensions.

It cannot match on other parts of the filename.


You could use JavaScript to read the filename and show an error if poor choice of file is selected.

document.querySelector('input').addEventListener("input", function () {
    if (this.files[0].name.match(/_32\..{3,4}$/)) {
        console.log("OK");
    } else {
        console.log("Not OK");
    }
});
<input type="file">

Upvotes: 4

Related Questions