-17

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

1

1 Answer 1

10

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) ) {
. . .

.

1
  • yes this is the regex, but how to validate the url, as example I have this path "/site/images/test.png". how to validate it ?
    – jobin
    Apr 19, 2015 at 16:54

Not the answer you're looking for? Browse other questions tagged or ask your own question.