How to allow input type=file accept only images

featured Image

In this short post, we will see how to allow the input field with type=file to accept only images from users.

Let’s say we have an input field as type=file.

<input id="upload" type="file">

Now, to make the input field accept only images, we add the accept attribute and specify the MIME type of files we want it to accept, here images.

If you want it to accept all formats of images, we can do this.

<input id="upload" type="file" accept="image/*">

This will allow it to accept all image formats like jpg, jpeg, png, gif, etc.

To make it accept only a specific image format, we can do this.

<input id="upload" type="file" accept="image/jpeg">

Now, it will only accept jpeg format images.

We can also add multiple image formats like this.

<input id="upload" type="file" accept="image/jpeg, image/png, image/gif">

It’s highly recommended to do a validation check on the server-side too once the file is uploaded from the client side.


Related Article:

Add CSS style to input type=file | Custom file input

Related Posts

featured Image

Disable scrolling Behavior on a body with element on position fixed in mobile

Here in this article, we will learn on how to disable the scroll behavior of the body whenever we scroll over an element whose position is fixed in our mobile…

Read more
featured Image

How to Remove Underline from Links in Bootstrap.

In this article, we will learn how to remove the underline from anchor tags in Bootstrap. Just like any other HTML element, links can also be styled using CSS. We…

Read more
featured Image

How to disable the resizable property of a textarea

In this article, we will see quick ways to disable the resizable property of a textarea using CSS. In CSS, we can disable it using the resize property. Set resize…

Read more

Leave a Reply

Your email address will not be published. Required fields are marked *