Skip to main content

How to Add Image Select Only Validation Rule in HTML?

Published: Asked By 1 Answers Answered

Learn how to add image select-only validation rule in HTML forms. Restrict file input to images using the accept attribute and JavaScript validation for better user experience.

Share:

1 Answer

S
Sooraj Community Contributor Answered on

If we use <input type="file">, it will accept all file types. But it is possible to restrict the file types to only images, or certain image file extensions. To achieve this, we need to use the HTML accept attribute. This attribute is only used with <input type="file"> and serves as a filter to select file inputs from the file input dialog box.

Example for image select only validation,

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the document</title>
  </head>
  <body>
    <input type="file" id="img" name="img" accept="image/*">
  </body>
</html>

 

Your Answer

Other Resources

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

Q&A Image
Q&A

Ask your coding-related doubts and get answers.

Certification Image
Certification

Earn certifications to enhance your resume.

internships Image
Internships

Hands-on projects to improve your skills.

Quiz Image
Quiz

Test your knowledge with interactive quizzes.

Interview Questions Image
Interview Questions

Prepare for interviews with curated question sets.

blog Image
Blogs

Add your technical blogs and read technical topics.

Certification Image
Certifications

Earn certifications to enhance your resume.