title: Use Sqlite ResultSet to Validate Form
body: <p>I am having a timing issue while trying to validate portions of my web form in Sqlite.  </p>
  
  <pre><code>function submitForm()
  {
     var isValid = validateK2();
     if(isValid)
        submitK2();
  }
  
  
  function validateK2()
  {
      db.transaction(function(tx){
          tx.executeSql("Select * From K2", [], function(tx, result){
               if (results meet my criteria)
                    return true;
               else
                    return false;
          }, onSqlError);
      });
  }
  </code></pre>
  
  <p>But at the time that the if (isValid...) code is executing, it is returning undefined because the validateK2 function has not finished executing and therefore has not yet returned true or false.  What am I missing here?</p>
  
  <p>EDIT:  The site is a portal allowing employees to pick from a menu of paperwork to fill out.  Upon submission, they may be submitting 1 form or 5.  So @CL is correct that I could just do my submitting in the executeSql callback rather than returning true and then submit.  But the fact that I might have any number more validations that need to run depending on the documents selected mean I need a dynamic way to validate each document and prior to "submitting" the form be able to check that all documents are valid.  So here is what I did:</p>
  
  <pre><code>var submitFinalTimeout;
  var doc1IsValid;
  var doc2IsValid;
  var selectedDocCount = selectedDocs.length \\selectedDocs is an array containing all the documents that have been selected and completed
  
  function ValidateForm()
  {
     submitFinalTimeout = window.setTimout(submitFinal, 3000);
  
     if(selectedDocs.indexOf("Doc1") != -1)
         validateDoc1();
     if(selectedDocs.indexOf("Doc2") != -1)
         validateDoc2();
  }
  
  function validateDoc1()
  {
      db.transaction(function(tx){
         tx.executeSql("Select * From Doc1Data", [], function(tx, result){
           if (results meet my criteria)
              doc1IsValid = true;
              selectedDocCount--;
              if(selectedDocCount != 0)
                {
                   window.cancelTimeout(submitFinalTimeout);
                   submitFinalTimeout = window.setTimeout(submitFinal, 3000);
                }
        }, onError);
      });
  }
  
  function validateDoc2()
  {
      db.transaction(function(tx){
         tx.executeSql("Select * From Doc2Data", [], function(tx, result){
           if (results meet my criteria)
              doc2IsValid = true;
              selectedDocCount--;
              if(selectedDocCount != 0)
                {
                   window.cancelTimeout(submitFinalTimeout);
                   submitFinalTimeout = window.setTimeout(submitFinal, 3000);
                }
        }, onError);
      });
  }
  
  function submitFinal()
  {
     if (doc1IsValid &amp;&amp; doc2IsValid)
         submitForm();
  }
  </code></pre>