0

I'm using an upload plugin to upload files in jquery.

This gets the type of the file uploaded (e.g. jpeg):

data.files[0].type

I need to check if it's a valid image file type (usal image file types jpeg, jpeg, gif, png) using a reg expression.

So far I have:

var isValid = (/\.(gif|jpg|jpeg|tiff|png)$/i).test(data.files[0].type);

But it's not working (always returns false).

Any ideas on how to fix?

Thanks

4
  • Why not use the jQuey validation plugin, namely the extension method? Commented Apr 10, 2013 at 11:06
  • Make sure data.files[0].type returns a string starting with a dot, like ".jpg". Try casting it as a string, maybe. Commented Apr 10, 2013 at 11:08
  • 2
    Just so that you're aware, that checks the file extension, not its type. Commented Apr 10, 2013 at 11:09
  • @adrianp it's not going to work with my image uplaoder plug in Commented Apr 10, 2013 at 11:11

2 Answers 2

3

If your type of the file uploaded (e.g. jpeg) without "dot" so you need that:

var isValid = (/(gif|jpg|jpeg|tiff|png)$/i).test(data.files[0].type)
Sign up to request clarification or add additional context in comments.

Comments

1

Your regexp is properly constructed (though it only checks extension, not type, as already mentioned), so it is obvious that you're checking wrong data.

Print out data.files[0].type and see if it really is what you think it is. Judging from variable name it is very likely that you have file extension without leading dot that your regexp checks for there.

1 Comment

Yes, it's without the . so it looks like 'jpeg' What do I need to do to my expression?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.