Validate file input type for mandatory presence in html
<p>I have a popup with a form containing file upload <code>cfgfile</code></p>

<pre><code>&lt;form id="mainForm" method="post" enctype="multipart/form-data" action=""&gt;
            &lt;input type="hidden" id="resulttype" value="Create"/&gt;
            &lt;input type="hidden" id="sessiontoken" name="sessiontoken" /&gt;
            &lt;input type="hidden" id="cfgid" name="cfgid"/&gt;
            &lt;img  id="titleImage" alt="title" src="/csm/view/images/creat_conform_title.gif" width="216" hspace="4" height="17" vspace="4"/&gt;&amp;nbsp;&amp;nbsp;
            &lt;table id="popupInfo" align="center" width="90%" border="0" cellspacing="0" cellpadding="0"&gt;
              &lt;tr&gt;
                &lt;td&gt;To create a new configuration,please download the CSMClient Utility from the "Download CSMClient" option.Run the utility in your environment and upload the generated output XML file.&lt;/td&gt;
              &lt;/tr&gt;
            &lt;/table&gt;&amp;nbsp;
            &lt;table id="downloadLinks" align="center" cellpadding="2" width="90%" border="0" cellspacing="0"&gt;
                &lt;tr&gt;&lt;td&gt;
                &lt;input name="Button1" type="button" onclick="openDownloadPage()"  id="button1" value="Download CSMClient" class="btn" /&gt;&amp;nbsp;
                &lt;a href="https://my-prod.informatica.com/infakb/howto/1/Pages/31738.aspx" target='_new' &gt;How to run CSMClient&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;
            &lt;/table&gt;
            &lt;table align="center" class="csm-table" width="90%" border="0" cellspacing="0" cellpadding="0"&gt;
                &lt;tr&gt;
                    &lt;td valign="middle" width="30%" &gt;Name &lt;/td&gt;
                    &lt;td width="70%"&gt;&lt;input type="text" id="cfgname" name="cfgname" tabindex="1" size="26" maxlength="64" value="" /&gt; &lt;/td&gt;
                &lt;/tr&gt;   
                &lt;tr&gt;
                  &lt;td valign="middle" height="65"&gt;Description &lt;/td&gt;
                  &lt;td height="65"&gt;
                  &lt;textarea type="text" class="dis_box" tabindex="2" id="cfgdesc" name="cfgdesc"  &gt;&lt;/textarea&gt;    
                  &lt;/td&gt;
                &lt;/tr&gt;
                &lt;tr valign="middle"&gt;&lt;tr&gt;
                &lt;td&gt;Type&lt;/td&gt;&lt;td&gt;
                &lt;select type="select" id="cfgenv" name="cfgenv" tabindex="3"&gt;
                    &lt;option value="Production" &gt;Production&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&lt;/option&gt;
                    &lt;option value="Development" &gt;Development&lt;/option&gt;
                    &lt;option value="Test/QA" &gt;Test/QA&lt;/option&gt;
                &lt;/select&gt;&lt;/td&gt;&lt;/tr&gt;
                &lt;tr valign="middle"&gt;&lt;td&gt;Upload CSMClient Output File&lt;/td&gt;
                    &lt;!-- &lt;td&gt;&lt;input type="file" id="cfgfile" name="cfgfile" tabindex="4"  class="multi"  maxlength="1" accept="xml" id="filefield" &gt;&lt;/td&gt;--&gt;
                    &lt;td id="filetd"&gt;&lt;input type="file" id="cfgfile" name="cfgfile" tabindex="4" /&gt;&lt;/td&gt;
                &lt;/tr&gt;
            &lt;/table&gt;
            &lt;table align="center" border="0" cellpadding="10" cellspacing="0" width="84%"&gt;
            &lt;tr&gt;
                &lt;td colspan="3" align="center"&gt;
                    &lt;label&gt;
                        &lt;input type="submit"  tabindex="5"  value="Create Configuration" id="btnSummary" class="btn" onClick="" /&gt;
                        &lt;!--&lt;input  type="button"  tabindex="5"  value="Create Configuration" id="btnSummary" class="btn" onClick="micoxUpload('postUploadInfo','Loading...','Error in upload'); return false;" /&gt;--&gt;
                        &lt;!--onClick="micoxUpload(this.form,'/csm/upload','postUploadInfo','Loading...','Error in upload'); return false;"--&gt;
                    &lt;/label&gt;
                        &amp;nbsp;
                    &lt;label&gt;
                        &lt;input type="button"  tabindex="6" value="Cancel" id="btnCancel" class="btn"/&gt;
                    &lt;/label&gt;
                &lt;/td&gt;
              &lt;/tr&gt;
            &lt;/table&gt;
        &lt;/form&gt;
</code></pre>

<p>When I reload the popup the previous value appears in it, and since we cannot set values for input file type I am removing the <code>cfgfile</code> and dynamically creating a new with same id.</p>

<pre><code>var fileholder = document.createElement("input");
fileholder.name="cfgfile";
fileholder.type="file";

$('#cfgfile').remove();
$('#filetd').empty();
$("#filetd").append(fileholder);
</code></pre>

<p>Till here everything is perfect. But how can I validate whether user has selected any file or not? I used below code, but all the time I get <code>EMPTY</code>. So summarizing how can I validate  my input file type to check whether user has entered anything or not.</p>

<pre><code>if($.trim($("#cfgfile").val()) === "")
    {
        alert("EMPTY");
        event.preventDefault();
    }
</code></pre>
