How to allow input type=file accept only images
Find how to allow input field type=file accept only a particular type of file, here only images from users.
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 Posts
How to create a horizontal list using HTML and CSS
Find out different ways to create horizontal list using inline-block and flex-box in CSS for your navigation bar on your website.
How to use calc() in tailwind CSS
Learn the use of calc() in tailwind CSS class. The calc() CSS function allows complex calculations to determine element sizes responsively.
Vertically Center Align a div in Tailwind CSS
Learn how to vertically center align a div in Tailwind CSS using Flexbox and absolute positioning and make your web development more efficient and fast.
Create a vertical line using Html and Css
Learn with step-by-step instruction on how to add vertical line in HTML using CSS border, transform and pseudo classes.
