Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

jQuery Validate required#dependency-expression

Posted on 2011-09-20
1
Medium Priority
?
876 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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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?
When the s#!t hits the fan, you don’t have time to look up who’s on call, draft emails, call collaborators, or send text messages. An instant chat window is definitely the way to go, especially one like HipChat. HipChat is a true business app. An…
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.
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 …

596 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