www.webdeveloper.com
Results 1 to 6 of 6

Thread: How to validate multiple input type=file with Javascript

  1. #1
    Join Date
    Oct 2008
    Posts
    6

    Exclamation How to validate multiple input type=file with Javascript

    Hello,
    I have a form which can contain dynamically 1 or a few input type=file fieldsm and I want to write a javascript validation which will tell the user to submit files in each field, if its blank, before submitting the form. I need this validation done on the client-side.

    Heres my non-working example, i think its only a syntax problem...

    <script language="javascript" type="application/javascript">
    function verify(file) {
    var f = document.my_form.file;
    if (f.value == '')
    {
    alert("Please choose a file.");
    return false;
    }
    }
    </script>

    <form action="_form.actions.php" name="my_form" enctype="multipart/form-data" method="post" />
    <input type="file" name="file1" id="file1">
    <input type="file" name="file2" id="file2">
    <input type="submit" value="submit" onClick="return verifier('file1');" >

    ****************************************************
    ultimately, the best function would be verifier('2') where ('2') would be the total number of input type=file in the form, then a "for" loop would go and check each of them. but i guess this would require to name each input as file[0], file[1], which is not a problem...

    any help would be GREATLY appreciated!

  2. #2
    Join Date
    Oct 2008
    Posts
    6
    I found this which seems to do the trick, can someone advise if ok?

    <script language="javascript" type="application/javascript">
    function verif() {
    var node_list = document.getElementsByTagName('input');
    for (var i = 0; i < node_list.length; i++) {
    var node = node_list[i];
    if (node.getAttribute('type') == 'file') {
    if (node.value == '') {
    alert("Please choose a file");
    return false;
    }
    }
    }
    }
    </script>

  3. #3
    Join Date
    Aug 2008
    Posts
    96
    it seems to be ok.. have you tested it?

  4. #4
    Join Date
    Oct 2008
    Posts
    6

    Thumbs up

    yeah i did, and it worked!

    FF doesnt give me any visible errors, havent tested IE yet...

    i spent at least 1 hour searching for an easy script to validate only a dynamic umber of <input type="file" ...> and this seemed to be the best solution.

  5. #5
    Join Date
    Oct 2008
    Posts
    6

    script OK in FF but not working in IE

    so the script works PERFECTLY in FF, but NOTHING HAPPENS in IE...

    can someone help me PLEASE?

    <script language="javascript" type="application/javascript">
    function verif() {
    var node_list = document.getElementsByTagName('input');
    for (var i = 0; i < node_list.length; i++) {
    var node = node_list[i];
    if (node.getAttribute('type') == 'file') {
    if (node.value == '') {
    alert("Please choose a file");
    return false;
    }
    }
    }
    }
    </script>

    i've never liked this browser incompatibility issue... everything to make our lives tougher! ARGH!

  6. #6
    Join Date
    Sep 2008
    Location
    Mexico
    Posts
    786
    post a link to the problem page or post the html

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles