title: jquery validation plugin : validate with some condition
body: <p>I m using jquery validator plugin. i need to validate some field with some condition. how do we apply that?</p>
  
  <p>i have a form where user select a country..then user creates branch on the fly,
  now i want to validate that if user do not select a county then prompt to select country and if user select a country then prompt to create atleast one branch.</p>
  
  <p>what i do as far</p>
  
  <p>if user create any branch then there  is a hidden field of counting branch. but that field is filled after creating branch, before that( on page load ) is has no value so if i validate for that hidden field then it always return true. what should i do</p>
  
  <p>here is code snippet i have used but i know there must be a better way to manipulate all above..please suggest me optimize way to do this</p>
  
  <pre><code>&lt;script type="text/javascript"&gt;
  jQuery.metadata.setType("attr", "validate");
  
  jQuery('#createForm').submit(function(){
          if(jQuery("#hasbranch").val()== 0 ){
              jQuery(".brancherror").html('Atleast one branch is necessary');
              return false;
          }
      });
  
  var validator=jQuery("#createForm").validate({
                 rules: { list of rules},
                 messgae : { list of messages },                                
  });
  &lt;/script&gt;
  
  &lt;form name="createForm" id="createForm"&gt;
  &lt;table&gt;
  &lt;tr&gt;
        &lt;td&gt;Select Country :&lt;/td&gt;
        &lt;td &gt;
        &lt;select name="country_id" id="country"  class="selectbox" onchange="return showBranch();"     title="Please select country" validate="required:true"  &gt;
      &lt;option value="" &gt;Select Country &lt;/option&gt;
      &lt;?php foreach($this-&gt;country as $key =&gt; $country) { ?&gt;
      &lt;option value="&lt;?php echo $country-&gt;id; ?&gt;"&lt;?php if ($country-&gt;id == $this-&gt;ad_data['country_id']) {?&gt; selected="selected" &lt;?php } ?&gt; &gt; &lt;?php echo $country-&gt;name; ?&gt;&lt;/option&gt; 
      &lt;?php } // end of foreach ?&gt;
        &lt;/select&gt;
        &lt;/td&gt;
    &lt;/tr&gt;
    &lt;tr&gt;
        &lt;td&gt; //Here is link to add branch (via iframe) &lt;/td&gt;
    &lt;/tr&gt;    
    &lt;/table&gt;
     &lt;input type="hidden" value="&lt;?php echo count($_SESSION['branches'])? 1: 0 ;?&gt; name="hasbranch" id="hasbranch" /&gt;
     &lt;/form&gt;  
  </code></pre>