Page 1 of 1

File Upload / Javascript Question

Posted: Tue Oct 05, 2004 11:05 am
by fangorn
I'm trying to restrict my users to only be able to upload PDF files.
This code below works for a single file.

Code: Select all

<script>
  function valid_form() &#123;
    var temp = document.sc_files.sd_file.value;

    if (temp.length > 0) &#123;
      if (temp.indexOf(".pdf") == -1) &#123;
        alert("Support docs must be PDF files.");
        return false;
      &#125;
    &#125;
  return true;
  &#125;
But I am now offering multiple files at once instead of just one and I used the PHP feature of have these files uploaded in an array.

Code: Select all

<tr><td><input type=file size=40 name=sd_files&#1111;]></td></tr>
<tr><td><input type=file size=40 name=sd_files&#1111;]></td></tr>

This works just fine for uploading the files. But I want to modify my JS validation to work with the multiple files. When I try to reference in JS:

Code: Select all

var temp = document.sc_files.sd_files&#1111;0].value;
This is undefined. Does anyone know which variable name in JS can be used with the array style input naming convention?

Posted: Tue Oct 05, 2004 11:14 am
by feyd

Code: Select all

document.forms&#1111;formname].elements&#1111;index]

Posted: Tue Oct 05, 2004 8:40 pm
by fangorn
aha, thanks alot!

test

Posted: Wed Oct 06, 2004 4:07 am
by phpScott
make sure you test that the element type is of file or you could get some odd things happening.

Posted: Wed Oct 06, 2004 4:59 am
by twigletmac
Don't forget to validate serverside as well :)

Mac