How to allow HTML file input to accept only image files?

Spread the love

Sometimes, we want to allow HTML file input to accept only image files.

In this article, we’ll look at how to allow HTML file input to accept only image files.

How to allow HTML file input to accept only image files?

To allow HTML file input to accept only image files, we set the accept attribute.

For instance, we write

<input type="file" name="myImage" accept="image/png, image/gif, image/jpeg" />

to set the accept attribute of the file input to image/png, image/gif, image/jpeg to make the file input accept images files with extensions png, gif, and jpeg.

Conclusion

To allow HTML file input to accept only image files, we set the accept attribute.

Leave a Reply

Your email address will not be published.