Solved

jQuery Validate required#dependency-expression

Posted on 2011-09-20
1
844 Views
Last Modified: 2013-11-19
Hi, I'm stuck on figuring out some jQuery validation...

I want to be able to have a text field ONLY required if a radio has been selected... can't quite figure it out....


<script type="text/javascript" src="http://jquery.bassistance.de/validate/lib/jquery.js"></script>
<script type="text/javascript" src="http://jquery.bassistance.de/validate/lib/jquery.metadata.js" ></script> 
<script type="text/javascript" src="http://jquery.bassistance.de/validate/jquery.validate.js" ></script> 

<script type="text/javascript">
// only for demo purposes
$.validator.setDefaults({
	submitHandler: function() {
		alert("submitted!");
	}
});
	
$.metadata.setType("attr", "validate");

$(document).ready(function() {
	$("#form1").validate();
});
</script>

<form id="form1" method="get" action="">
  <input type="radio" id="gender" value="m" name="gender" /> Male<br>
  <input type="radio" id="gender" value="f" name="gender" />Female<br>
  <input type="text" name="txt" id="txt" validate="required:'#gender:selected'" /><br>
  <input class="submit" type="submit" value="Submit"/><br>
</form>

Open in new window




Thanks in advance!
0
Comment
Question by:dev09
1 Comment
 
LVL 82

Accepted Solution

by:
leakim971 earned 500 total points
ID: 36570847
what about : http://jsfiddle.net/SDDPM/

// only for demo purposes
$.validator.setDefaults({
    submitHandler: function() {
        alert("submitted!");
    }
});
    
$.metadata.setType("attr", "validate");

$(document).ready(function() {
    $(":radio[name='gender']").click(function() { 
        $("#txt").addClass("required");
    })
    $("#form1").validate();
});

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Any business that wants to seriously grow needs to keep the needs and desires of an international audience of their websites in mind. Making a website friendly to international users isn’t prohibitively expensive and can provide an incredible return…
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 how to count occurrences of each item in an array.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…

911 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

24 Experts available now in Live!

Get 1:1 Help Now