Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
123
rated 0 times [  124] [ 1]  / answers: 1 / hits: 26310  / 9 Years ago, sun, april 19, 2015, 12:00:00

How to validate image file extension (jpg,png,gif,bmp) with JavaScript regular expression?


More From » regex

 Answers
21

Accordingly your issue the regular expression is quite simple.



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


Do you really sure that nothing else will be used? For example, JPEG format allows both .jpg and .jpeg extensions. That's why I put e? pattern in the regular expression.



Example of validation could be as follows:



var filename = /site/images/test.png;
if ( /.(jpe?g|png|gif|bmp)$/i.test(filename) ) {
. . .


.


[#67012] Thursday, April 16, 2015, 9 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
ronaldodarioc

Total Points: 441
Total Questions: 105
Total Answers: 106

Location: Christmas Island
Member since Sun, Mar 7, 2021
3 Years ago
;