Solved

jQuery Validate required#dependency-expression

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to print dynamic Google map and directions? 1 15
asp.net mvc5 6 17
QQ problem 22 26
Html Table Looping (part 2) 5 12
This article discusses how to create an extensible mechanism for linked drop downs.
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

825 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