Very simple HTML form with Javascript Validation - Help Please =]

Discussion in 'JavaScript' started by iamays, Dec 4, 2008.

  1. #1
    Ok, this might be simple for some of you. I have a form on an html page with two file inputs. I don't want to upload those files to my server, I just want the form to validate whether or not an image was selected when they press submit. Then it will forward the user to another html page.

    Right now I'm just trying to get it to validate if they have chosen anything at all, but I would prefer it to validate it to be an image file. Keep in mind, the form will do absolutely nothing with the image files, I just want it to validate if they have chosen two image files.


    Here's all I have so far:

    <script type="text/javascript">
    function validate(formCheck) 
    {
    if (formCheck.image1.value = "")
    {
    alert("Please select an image");
    formCheck.image1.focus();
    return false;
    }
    if (formCheck.image2.value = "")
    {
    alert("Please select an image");
    formCheck.image2.focus();
    return false;
    }
    return true;
    }
    </script>
    
    <FORM name="forma" METHOD="LINK" enctype="multipart/form-data" ACTION="test.html">
    
      &nbsp;<H1>Photo1:</H1>
      	    
                  <p>Please choose an image file</p>
                  <p> 
                    <input name="image1" type="file" size="30" />
                  </p>
    
                  <p>&nbsp;</p>
         
    
                <H1>Photo2:</H1>
         
                  <p>Please choose an image file</p>
                  <p> 
                    <input name="image2" type="file" size="30" />
                 
                  </p>
                  <br><br>
                  <p> 
          
    <INPUT TYPE="submit" VALUE="Submit" onClick="return validate(forma)";>
    </FORM>
    Code (markup):

    I've searched all over and have tried many different scripts out, and just can't seem to get this to work. Any help would be much appreciated! Thanks!
     
    iamays, Dec 4, 2008 IP
  2. dimitar christoff

    dimitar christoff Active Member

    Messages:
    882
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    90
    #2
    it is difficult to control the content type of a browse interface which is essentially an OS element.

    search no further than SWIFF fancy upload - http://digitarald.de/project/fancyupload/ - it uses a small flash embed to analyse the files and it can place all sorts of restrictions on them--like file size, type etc. it can also queue multiple uploads under 1 handler and gives progress report per file and overall

    good luck.
     
    dimitar christoff, Dec 5, 2008 IP
  3. iamays

    iamays Banned

    Messages:
    528
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    0
    #3
    thank you for your recommendation, but I'd rather stay away from flash and any type of advanced file upload system. This script is mainly for entertainment purposes and it is not required to actually handle the selected files. All that is necessary, is for the user to select a file (preferably an image) and then the form directs the user to another page. I want the script to alert the user if they have not selected an image in the first box, and alert them if they haven't selected an image in the second box. if both boxes have an image selected, once they hit submit it will take them to the other page.
     
    iamays, Dec 5, 2008 IP