+1  A: 

Looks to me like it is going to compare the file extension against the first item in the list (.jpeg != .jpg) and return 0. This means it won't get a chance to try the second extension in the list.

Probably best to set a flag in the comparison and not return until all items in the extension list have been compared against the file extension.

Corin
+6  A: 

Should be

for (i = 0; i < extensions.length; i++) {
    if (extensions[i] == final_ext) {
        return 1;
    }
}
return 0;

Note also that return immediately ends the current function, so putting a break after return is pointless.

Finally, if this Javascript is running on the client-side (i.e. in a web browser) keep in mind that the user may circumvent this (e.g. by using Firebug). You may wish to do checking at the server side too.

Artelius
+1 Yup, it should be this. Better answer than mine.
Corin
yep I got server side checking too...
jamal
+1  A: 

Your logic is wrong. This is what the loop should look like:

for (i = 0; i < extensions.length; i++) {
    if (extensions[i] == final_ext) {
        return 1;
    }
}
return 0;
Franz
Your logic is wrong also. See my post.
Artelius
Oops. Error. Fixed it.
Franz
Lol. You added the return 0 statement at the end, too. Voted yours up ;)
Franz
Great minds and all that ...
tvanover
@tvanover: Took the words out of my mouth.
Artelius
+3  A: 

Try a regular expression instead

function validate(x) {
    return /.(jpg|jpeg)$/ig.test(x);
}
Matt Smith
I was going to say to use the split method, but in this case a regular expression does indeed make more sense :)
AntonioCS
Thanks for your help guys but matt's code work on my situation
jamal
A: 

Try something like this:

VALID_EXT = 'jpeg jpg';
function validate(x) {
  return +(VALID_EXT.split(/\s+/).indexOf(x.replace(/^.*\./,'').toLowerCase()) >= 0)
}
validate('file.jpeg');     // 1
validate('file.jpg');      // 1
validate('file.pic.jpg');  // 1
validate('file.gif');      // 0
Jason Harwig