Conditional Validation with jQuery Validation Plugin - Conditionally Require Field Value

by Admin 21. January 2013 19:46

Hey Guys,

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.

Tags: , ,

Web Design Tips

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

© 2014 FMR Web Design Delray Beach, Florida | P: (561) 235-3775 | E: info@fmrwebdesign.com