Solved

jQuery Validate required#dependency-expression

Posted on 2011-09-20
1
841 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
"In order to have an organized way for empathy mapping, we rely on a psychological model and trying to model it in a simple way, so we will split the board to three section for each persona and a scenario and try to see what those personas would Do,…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

743 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

11 Experts available now in Live!

Get 1:1 Help Now