-17

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

Jonas Czech
  • 12,018
  • 6
  • 44
  • 65
jobin
  • 1,119
  • 2
  • 12
  • 22

1 Answers1

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

.

jsxt
  • 1,097
  • 11
  • 28
  • 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 '15 at 16:54