mkb
mkb

Reputation: 81

Validate input type="file" field using javascript

I have a form in which the user first select if they want to upload any file. If yes then 3 upload box are displayed to them.

On click of submit there is a JavaScript function which checks for any empty fields. In this function, if the option to upload file is selected, then I am checking if the input type="file" is empty.

The error I am facing is that even after the user select a file for upload, the error message to upload a file is still displayed.

Here is my HTML code:

<!-- 3rd Fieldset STARTS -->
<fieldset class="fieldSetSpace">
    <legend class="legendText">&nbsp; Upload Documents &nbsp;</legend>

    <span id="yes"><input type="radio" name="rdoUploadDocu" id="rdoUploadDocuYes" tabindex="23" value="yes" onClick="javascript: showUploadDiv();" /></span>
    &nbsp;Yes I have A Documents To Upload

    <div id="divUploadDoc" style="display:none;">
        <span class="contact_table">Upload Document 1 </span>

        <input type="file" name="files[]" id="file1" class="txtCoName" />

        <span class="contact_table">Upload Document 2</span>

        <input type="file" name="files[]" id="file2" class="txtCoName" />

        <span class="contact_table">Upload Document 3</span>

        <input type="file" name="files[]" id="file3" class="txtCoName" />
    </div>
    <?php echo $errorResumeUpload; ?>
    <br />
    <span id="no"><input type="radio" name="rdoUploadDocu" id="rdoUploadDocuNo" value="no" tabindex="24" onClick="javascript: hideUploadDiv();" /></span>
    &nbsp;No I do not have A Documents To Upload

    <div id="divUploadCheckError" class="divError"></div>

</fieldset>         
<!-- 3rd Fieldset ENDS -->

Here is my JS function:

else if (document.getElementById('rdoUploadDocuYes').checked) 
{       
    var upload1 =  document.getElementById('file1').value;
    var upload2 =  document.getElementById('file2').value;
    var upload3 =  document.getElementById('file3').value;

    alert( upload1 );
    alert( upload2 );
    alert( upload3 );

    if( ( upload1 == '' ) || ( upload2 == '' ) || ( upload3 == '' ) )
    {
        var objErrDiv = document.getElementById('divUploadCheckError');
        objErrDiv.innerHTML= 'Please upload at least one documents ';
        objErrDiv.style.padding='4px 4px';
        objErrDiv.style.visibility='visible';
        objErrDiv.style.margin='10px 0px 2px 0px';
        return false;
    }
    else
    {
        return false;
    }
}

Any help will be appreciated.

Upvotes: 1

Views: 22976

Answers (1)

Praveen
Praveen

Reputation: 56501

I think you're missing to reset the innerHTML of objErrDiv div.

else if (document.getElementById('rdoUploadDocuYes').checked) 
{       
    var upload1 =  document.getElementById('file1').value;
    var upload2 =  document.getElementById('file2').value;
    var upload3 =  document.getElementById('file3').value;
    var objErrDiv = document.getElementById('divUploadCheckError');

    alert( upload1 );
    alert( upload2 );
    alert( upload3 );

    if( upload1 == '' )
    {

        objErrDiv.innerHTML= 'Please upload at least one documents ';
        objErrDiv.style.padding='4px 4px';
        objErrDiv.style.visibility='visible';
        objErrDiv.style.margin='10px 0px 2px 0px';
        return false;
    }
    else
    {
        objErrDiv.innerHTML="";  // Try adding this
        return false;
    }
}

Upvotes: 3

Related Questions