views:

27

answers:

2

With a html type=file, how can I validate to determine if a file was uploaded, (for client side validation using javascript or jquery)

<form action="program" enctype="multipart/form-data" method="post">
<input type="text" name="textline" size="30">
<input type="file" name="datafile">
<input type="submit" value="Send">
</form>

<script>
 if (something ) 
    message = "You did not upload a file...";
</script>           
+2  A: 

Same way as any other input, look at the value. For example

<form id="programform" action="program" enctype=...

<script type="text/javascript">
    document.getElementById('programform').onsubmit= function() {
        if (this.elements.textline.value==='')
            alert('Please enter a description.');
        if (this.elements.datafile.value==='')
            alert('Please choose a file to upload.');
        else
            return true;
        return false;
    }
</script>
bobince
A: 

Thanks for the push in the right direction... Here is what I was looking for.

<form name="registration_2" id="registration_b" action="registration_b.php" method="post"  nctype="multipart/form-data" accept-charset="UTF-8" onsubmit="return check_checkboxes();"> 
<input type="text" name="textline" id="textline" size="30">
<input type="file" name="datafile"   id="school_logo_id">
<input name="no_school_logo_id"  id="no_school_logo_id"  type="checkbox" onclick="no_school_logo(this);"/>Our school does not have a logo
<input type="submit" value="Send">
</form>



<script type="text/javascript">
function check_checkboxes()
{
    var logo = document.getElementById('school_logo_id');
    var nologo = document.getElementById('no_school_logo_id');

        if (logo.value==='' && nologo.checked===false)
            alert('Must enter logo file or check no file available.');          

}
</script>
bob