Conditional Validation with jQuery Validation Plugin – Conditionally Require Field Value

So here is another tip for jQuery Validation Plugin. Sometimes you might require a value for the field based on the value of some other field. e.g. you may have contact form with two radio buttons for contact preference: email or phone. Now based on what they select you might want to require a value for the email field or the phone field. So how do we solve this problem? Pretty Simple!

rules: {
  email: {
  required: function(){
    return $('#prefEmail').is(':checked');
  }
},
  phone: {
    required: function(){
      return $('#prefPhone').is(':checked');
   }
  }
}

So instead of the rule email : {required : true} or phone: {required : true} you use the code above. The function will return true or false base on if the radio button is selected.

If you need help with your website contact FMR Web Design in Boca Raton. We have helpe many businesses in Boca Raton area with their online projects.

Leave a Reply

Your email address will not be published. Required fields are marked *