I need a regular expression to validate image file extensions in javascript. Do you have one ?
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.
onload = imageIsLoaded; reader. readAsDataURL(this. files[0]); } } else alert('This is not an image file! '); }); function imageIsLoaded(e) { result = e.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With