Solved

jQuery Validate and depends

Posted on 2010-11-09
1
1,452 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In Part 1 (http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/A_7849-Hex-Maze.html) we covered the hexagonal maze basics -- how the cells are represented in a JavaScript array and how the maze is displayed.  In this part, we'…
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…
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…

688 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