Solved

jQuery Validate and depends

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

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article discusses how to create an extensible mechanism for linked drop downs.
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now