Solved

jQuery Validate and depends

Posted on 2010-11-09
1
1,431 Views
Last Modified: 2012-05-10
I am using jQuery validate to validate a form.  I have mulitple fields that are required if a select box is a certain value.
do i have to do this (see code)
employee_firstname: {
						required: {
							depends: function(element){
								if($("#person_responsible").val()=='6'){
									return true;
								} else {
									return false;	
								}
							}
						}
					}

Open in new window


for eac of the dependent fileds or is there a way to say all these other fields are required if the select is a certain value
Thanks
0
Comment
Question by:jduawa
1 Comment
 
LVL 16

Accepted Solution

by:
jmatix earned 500 total points
ID: 34096564
If you want to apply the same rule for multiple elements with names starting with, say, "employee" you can add this code to your $(document).ready() function:
$(document).ready(function(){ 
$("input[name^=employee]").each(function(){
	$(this).rules("add", {
		required: {
		depends: function(elem){$("#person_responsible").val()=='6';}},
		messages: {
			required: "This field is required"
		}
	} );			
});

Open in new window

0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question