1

I am not really sure how to word the question, but I basically have an old javascript that checks file extensions of a user upload BEFORE it is sent to the server for processing. (Just for the admin section of a website.)

Well, I want a div to slideDown() whenever a javascript condition returns true inside a loop (meaning, the file is good to upload).

Code so far:

  var thisext = fieldvalue.substr(fieldvalue.lastIndexOf('.'));
for(var i = 0; i < extension.length; i++) {
    if(thisext == extension[i]) { return true;

            $("#pleasewait").slideDown();

         }
    }
alert("Please upload ONLY .mp3 files. No other files will work.");
return false;
}
5
  • 1
    You don't want to just rely on JavaScript to check for this. Someone could simply turn off their JavaScript, or override your function, or do whatever else they want with their client-side code, and upload a random file anyway. If you don't check for this in your server-side code, your app may blow up. Commented Jan 22, 2012 at 4:52
  • Why do you care if the files are named .mp3, or .mpeg3 or .mpeg2layer3 or .flubber? Commented Jan 22, 2012 at 4:55
  • @voithos I know. Agreed. But like I said, this is in a website password protected main admin section. And also, imagine hitting the upload button and then waiting 50 seconds only to see "Not valid file type." Thats why a javascript check FIRST and then the PHP check LATER is more user friendly. Commented Jan 22, 2012 at 19:56
  • @sarnold I care because the script that plays the music only uses .mp3 files, and that is it. Commented Jan 22, 2012 at 19:57
  • @PaulHanak: Most definitely; client-side for user-friendliness, and server-side for program correctness. Commented Jan 22, 2012 at 20:34

2 Answers 2

5

You should put return true; after $("#pleasewait").slideDown();

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

Comments

3

The code has a textbook case of a logical error. The slidedown should occur before the return statement because the function will exit immediately after that line.

Try this within your function:

var thisext = fieldvalue.substr(fieldvalue.lastIndexOf('.'));
for(var i = 0; i < extension.length; i++) {
    if(thisext == extension[i]) { 
        $("#pleasewait").slideDown();

        return true;
     }
}

alert("Please upload ONLY .mp3 files. No other files will work.");
return false;

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.