Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validade in HTML5 to only allow in 'input type="file"' JPG, GIF or PNG? [duplicate]

Tags:

html

regex

Possible Duplicate:
Limit file format when using <input type=“file”>?

I need to use the HTML5 pattern attribute to only allow JPG, GIF and PNG files in a <input type="file"> element.

I currently have this pattern, but it is not working:

<input type="file" class="input-file" id="tbinstUploadFile0" name="tbinstUploadFile0" placeholder="Please insert an image file..." pattern="([^\s]+(\.(?i)(jpg|png|gif|bmp))$)"> 

This regex pattern is not compatible with HTML5? How can I test for valid HTML5 regex patterns?

like image 855
André Avatar asked Jun 19 '12 08:06

André


People also ask

How do you make an input type file accept only images?

Using type="file" and accept="image/*" (or the format you want), allow the user to chose a file with specific format.

How do you specify a file type in HTML input?

The <input type="file"> defines a file-select field and a "Browse" button for file uploads. To define a file-select field that allows multiple files to be selected, add the multiple attribute. Tip: Always add the <label> tag for best accessibility practices!

Which attribute is only valid for image type input in HTML?

Definition and Usage. The accept attribute specifies a filter for what file types the user can pick from the file input dialog box. Note: The accept attribute can only be used with <input type="file"> .


1 Answers

Try something like

<input type="file" name="my-image" id="image" accept="image/gif, image/jpeg, image/png" /> 

Click here for the latest browser compatibility table

Live demo here

To select only image files, you can use this accept="image/*"

<input type="file" name="my-image" id="image" accept="image/*" /> 

Live demo here

Only gif, jpg and png will be shown, screen grab from Chrome version 44 Only gif, jpg and png will be shown, screen grab from Chrome version 44

like image 73
kiranvj Avatar answered Oct 05 '22 23:10

kiranvj