Solved

jQuery Validate and depends

Posted on 2010-11-09
1
1,412 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

When you need to keep track of a simple list of numbers or strings, the Array object is your most direct tool.  As we saw in my earlier EE Article (http://www.experts-exchange.com/A_3488.html), typical array handling might look like this: (CODE) B…
In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
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…

773 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