Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Regex JavaScript image file extension [closed]

I need a regular expression to validate image file extensions in javascript. Do you have one ?

like image 510
prcaen Avatar asked May 06 '12 18:05

prcaen


People also ask

How to validate file extension in JavaScript?

Using JavaScript, you can easily check the selected file extension with allowed file extensions and can restrict the user to upload only the allowed file types. For this we will use fileValidation() function. We will create fileValidation() function that contains the complete file type validation code.

How do you check if a file is an image JavaScript?

onload = imageIsLoaded; reader. readAsDataURL(this. files[0]); } } else alert('This is not an image file! '); }); function imageIsLoaded(e) { result = e.


1 Answers

Could you explain the purpose?

Anyway here's one assuming you support few image types.

(/\.(gif|jpe?g|tiff?|png|webp|bmp)$/i).test(filename) 

I have put the whole regular expression within parentheses () so as to disambiguate between the slash (/) operator and RegExp object. See JSLint for more details.

Here's the raw regex as well.

/\.(gif|jpe?g|tiff?|png|webp|bmp)$/i 

This Regex also assumes that you're including the dot before the extension. Remove the \. if you're not.

like image 122
g13n Avatar answered Sep 18 '22 09:09

g13n