title: validation not working properly when adding new row?
body: <p>I have a table with each row contains a 'delete link' and for the entire table a <code>add additional file link</code> is there at the bottom of table.</p>
  
  <p>Whenever you press <code>add additional link</code> it will add a new row(i.e a label(called file) and browse button and a text box called <code>description</code>) but before adding new row when you press on add link it will first validate whether every field in row got required values or not if a file has been uploaded and text entered in <code>description</code> text box then it will add a new empty row other wise it will ask for values to enter in fields.</p>
  
  <p>But my problem is when I am adding two rows perfectly then if I delete the first row and then if press add additional link twice then it is adding two empty rows but it should not.ONLY ONE ROW SHOULD BE ADDED WHEN PRESSED ON ADD ADDITIONAL LINK AND IF YOU PRESS ON IT AGAIN THEN IT SHOULD VALIDATE THE FIELDS WHETHER THEY HOLD THE VALID VALUES OR NOT,IF THEY ARE FINE THEN ONLY IT SHOULD ALLOW THE USER TO ADD A NEW ROW BELOW THAT AND THE PROCESS MUST GO AS SUCH.BUT IT SHOULD NOT ALLOW THE USER TO ADD TWO NEW EMPTY ROWS.</p>
  
  <p>If the last row was filled with proper values then only another row should be added....can any one please help me regarding this issue. Here my code goes:</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  var addDiv = $('#listfiles');
  var i = $('#addinput p').size() + 1;
  $('#addNew')
  .live(
      'click',
      function(event) {
          flag = true;
          var row_len = $('#listfiles tr').length;
          var cnt = parseInt($("#noOfFiles").val());
          cnt=row_len;
          var targetId = $('#newfileid').val();
  
      var descrId = $('#newdescrid').val();
  
      var fileName = $("#" + targetId).val();
      var descrName = $("#" + descrId).val();
  if(descrName == undefined)
      {
      descrName="";
      }
      descrName = $.trim(descrName);
      if (fileName != undefined) {
      if (fileName.length == 0) {
          $("#error").text("Please add a new file for browse button.
        Remove file row if file isn't to be uploaded.").show();
  $("#error").css("color", "red");
          event.preventDefault();
  } else
  
  if (!(fileName.match(/\.(?:csv)$/))) {
  
  $("#error").text("Uploaded file(s) must be in CSV format. Please 
      upload a new file.").show();
  $("#error").css("color", "red");
  event.preventDefault();
  } else if (descrName.length == 0|| descrName == '') {
  $("#error").text("Please add a new description for the File.").show();
  $("#error").css("color", "red");
  event.preventDefault();
  } else {
      alert("1st else");
  $(
      '&lt;tr class="filerow"&gt;'
                                                  + '&lt;td width="265px"&gt;'
                                                  + '&lt;span class="browse-text"&gt;File '
                                                  + (cnt + 1)
                                                  + ':&lt;/span&gt;'
                                                  + '&lt;span class="browsebutton"&gt; &lt;input type="file" 
      id="addfile' +  cnt   + '" name="ivrsFiles[' +  cnt   + '].
      file"  value="ivrsFiles[' + cnt + '].file"/&gt;&lt;span class="description"&gt;
      &lt;input type=text &gt;&lt;/span&gt;&lt;/span&gt;'
                                                  + '&lt;/td&gt;'
                                                  + '&lt;td&gt;'
                                                  + 'Description:&amp;nbsp;&lt;input type="text" id="descr' +  
      cnt   + '" name="ivrsFiles[' +  cnt   + '].fileDescription" value=""/&gt;'
                                                  + '&lt;td valign="middle"&gt;&lt;a id="remNew1" name="remNew[' + cnt 
       + ']"  href="#" class="delete-file"&gt;Delete file&lt;/a&gt;&lt;/td&gt;'
                                                  + '&lt;/td&gt;' + '&lt;/tr&gt;')
  .appendTo(addDiv);
  $("#newfileid").attr("value",'addfile' + cnt);
  $("#newdescrid").attr("value",'descr' + cnt);
  cnt = cnt + 1;
  $("#noOfFiles").attr("value", cnt);
      return false;
  }
  } else if (fileName == undefined) { 
          alert("2nd else");
      $(
      '&lt;tr class="filerow"&gt;'
                                              + '&lt;td width="265px"&gt;'
                                              + '&lt;span class="browse-text"&gt;File '
                                              + (cnt + 1)
                                              + ':&lt;/span&gt;'
                                              + ' &lt;span class="browsebutton"&gt;&lt;input type="file" id="addfile' +
        cnt   + '" name="ivrsFiles[' +  cnt   + '].file" value="ivrsFiles
       [' + cnt + '].file"/&gt;&lt;span class="description"&gt;&lt;input type=text&gt;&lt;/span&gt;&lt;/span&gt;'
                                              + '&lt;/td&gt;'
                                              + '&lt;td&gt;'
                                              + 'Description:&amp;nbsp;&lt;input type="text" id="descr' +  cnt   + '"
      name="ivrsFiles[' +  cnt   + '].fileDescription" value=""/&gt;'
                                              + '&lt;td valign="middle"&gt;&lt;a id="remNew1" name="remNew[' + cnt  + ']" 
      href="#" class="delete-file"&gt;Delete file&lt;/a&gt;&lt;/td&gt;'
                                              + '&lt;/td&gt;' + '&lt;/tr&gt;').appendTo(
  addDiv);
  $("#newfileid").attr("value", 'addfile' + cnt);
  $("#newdescrid").attr("value", 'descr' + cnt);
  cnt = cnt + 1;
  $("#noOfFiles").attr("value", cnt);
  return false;
      }
        });
  
      $('#remNew1')
          .live(
          'click',
  function() {    
  var targetId = $('#newfileid').val();
  var fileName = $("#" + targetId).val();
  var cnt = parseInt($("#noOfFiles").val());
  var agree = confirm("Are you sure? If you choose OK, This Template
        will be Removed");
  if (agree) {
  $(this).parent().parent().remove();
   $('#listfiles tr').each(function (i) {
  var index = i + 1;
  $('td:first .browse-text', this).text('File ' + index + ':');
         });
  somethingChanged = true;
  cnt = cnt - 1;
     $("#noOfFiles").attr("value", cnt);
  if (fileName == undefined) {
      window.location = "files_formulae.htm?projectId="
      + $
      (
      project.projectId
      )
      ;
      } 
  } else
      return false;
       });
  
      });
  &lt;/script&gt;
  &lt;body&gt;
  &lt;table class="filetable" cellpadding='8' cellspacing='5'id="listfiles"&gt;
  
  &lt;c:forEach var="ivrsFile" items="${project.ivrsFiles}"varStatus="status"&gt;
  &lt;tr id="filerow"&gt;
  &lt;td width='265px' &gt;&lt;span class="browse-text"&gt;File &lt;c:out
       value='${status.index + 1}' /&gt;:&lt;/span&gt;
  &lt;c:if   test="${fn:length(ivrsFile.fileName) &gt; 0}"&gt;
  &lt;form:input id="addFile${status.index}"
  path="ivrsFiles[${status.index}].fileName" readonly="readonly"/&gt;
  &lt;form:hidden path="ivrsFiles[${status.index}].fileID" /&gt;
  &lt;/c:if&gt; 
  &lt;c:if test="${fn:length(ivrsFile.fileName) &lt;= 0}"&gt;
      &lt;form:hidden path="ivrsFiles[${status.index}].fileName" /&gt;
  &lt;span class="browsebutton"&gt; &lt;input style="color: #F67D08"  type="file" 
  id="addFile${status.index}"
  name="ivrsFiles[${status.index}].file"
  value="ivrsFiles[${status.index}].file" /&gt;
       &lt;span class="description"&gt;&lt;input   type=text&gt;&lt;/input&gt;&lt;/span&gt;&lt;/span&gt;
      &lt;form:hidden path="ivrsFiles[${status.index}].fileID" /&gt;
     &lt;/c:if&gt;
  &lt;/td&gt;
  &lt;td &gt;Description: &lt;form:input id="descr${status.index}"
  path="ivrsFiles[${status.index}].fileDescription" /&gt;&lt;/td&gt;
  
  &lt;td valign='middle'&gt;&lt;c:if test="${ivrsFile.fileID ne 0 }"&gt;
  &lt;a id="remNew"href="files_delete.htm?delete=yes&amp;projectId=
      ${project.projectId}&amp; fileID=${ivrsFile.fileID}"
  onclick="return confirmDelete('${project.projectId}');"
  class='delete-file'&gt;Delete file&lt;/a&gt;
  &lt;/c:if&gt; &lt;c:if test="${ivrsFile.fileID eq 0 }"&gt;
  &lt;a id="remNew1" href="#"  class='delete-file'&gt;Delete file&lt;/a&gt;
  &lt;/c:if&gt;&lt;/td&gt;
  &lt;/tr&gt;
  &lt;/c:forEach&gt;
       &lt;/table&gt;
  &lt;a href="#" id="addNew" class='add-file'&gt;Add additional file&lt;/a&gt;
  &lt;/body&gt;
  </code></pre>