Robin
Robin

Reputation: 517

jquery validate - make either of 2 fields required on submit

I'm working with jQuery validate to validate my forms, but i'd like to know if it's possible to make either of 2 fields required. I only know ways to make 1 required, but either of them should be required.

Could anyone point me to the right direction?

Upvotes: 2

Views: 4439

Answers (1)

Sibu
Sibu

Reputation: 4617

Try like this

<form id="from1" name="from1"   method="post">
      <input type='text' id='textbox1' name='textbox1'/>
      <input type='text' id='textbox2' name='textbox2' />
      <input type='submit' value='submit' id='save' name='save'/>
</form>​


    <script>
    var gobalvar=false; 

    $("#from1").validate({ 
      rules: {
            textbox2:{required: function(){
                if($('#textbox1').val()=="")//return value based on textbox1 status
                     return true;
                else
                     return false;
               }
            } 
        },
            messages: {
            textbox2:'required'
            }
    });
</script>

DEMO

Upvotes: 7

Related Questions