0

Im trying to check an input files extension with JavaScript, using:

if(extension != 'jpeg, JPEG, JPG, jpg'){}

I want the statement to read logically :'if variable extension doesn't equal jpeg, or JPEG, or..' and so on

but this doesn't work, how do I code this properly?

2
  • 7
    That's Javascript, not jQuery. Commented Feb 13, 2013 at 22:06
  • 2
    shrink down the check, use toLowerCase()! Commented Feb 13, 2013 at 22:12

5 Answers 5

2

If you don't want to use regex you can do something like:

extension = extension.toLowerCase();
if(extension != 'jpeg' && extension != 'jpg')

This will also filter file extensions like JpEg.

Sign up to request clarification or add additional context in comments.

2 Comments

Why wouldn't you want to use regex for this, though? "If you don't want to use a hammer, you can bash that nail in with a rock."
Because well... almost no one likes regex :-"
1
if(extension != 'jpeg'|| extension != 'JPEG' || extension != 'JPG' || extension !='jpg'){
    // would do the magic for you ;-)    
}

It's simple javascript not jquery!

1 Comment

That expression is always true. Use and instead of or.
1

This is a little cleaner since you can define the array of acceptable extensions elsewhere.

if ($.inArray(extension.toLowerCase(), ["jpeg", "jpg"]) !== -1) {
  // do something
}

Comments

0
if(extension != 'jpeg' ||  extension !=  'JPEG' || extension != 'JPG' extension != 'jpg'){}

1 Comment

That expression is always true. Use and instead of or.
0

Using a regular expression here will make your life easier:

if (!extension.match(/^jpe?g$/i)) {
    alert("Bad extension");
}

Comments

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.