Solved

jQuery Validate required#dependency-expression

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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

An enjoyable and seamless user experience can go a long way on an eCommerce site. While a cohesive layout and engaging copy play roles in creating a positive user experience, some sites neglect aspects that seem marginal but in actuality prove very …
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

696 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