title: JQuery: Validation On The Fly
body: <p>I have a script which calls a php file and gets the proper fields to show each time a select box (category) is changed. Along with each row, there is a value (0 or 1) to show if that field is required or not. If you look below, the each() function on data shows "key" being the row, and "val" being whether it's required or not.</p>
  
  <p>I currently check to see if it's required, and then push the "key" into an array.Now, the tricky part, I want to modify the validate function to include all of the elements in the array "req" (essentially building the validation requirements on the fly). Currently, only "category" is required (it needs to be added to the "rules" section of validate)     </p>
  
  <pre><code>$('#category').change(function() {
      var cid = $(this).val();
  
      $.get('./ajax/get_cat_info.php?cid=' +cid, function(data, textStatus) {
  
          var req = new Array();
  
          $.each(data, function(key, val) {
  
              $('#' + key).show();
              if(val == 1) {
                  req.push(key);
              }
           });
      }, 'json');
  
      // Validation:
      $("#submissionForm").validate({
          rules: {
              category: "required"
          },
          messages: {
              category: ""
          }
      }); 
  });
  </code></pre>