Solved

jQuery - validating a field based on another field

Posted on 2011-09-27
2
281 Views
Last Modified: 2012-06-27
Hi,

I am trying to validate a field based on wether there is a value entered in another field. There are three fields in three rows. The first row requires all fields to have a value. The second and third row only require the title field to be assigned a value if the name field is used. Please see the attached .htm file. At the moment the form all the title fields are being vaildated, despite there being no values in name2 and name3.

Any help would be greatly appreciated.
<html>

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Employee ID</title>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script>
$(document).ready(function(){
        $("#registerForm").validate({
        rules: {
               id1: "required",
               name1: "required",
               title1: "required",
               title2: {
				required: function(element) {
				return $("#name2").val();
							}
					}, 
               title3: {
				required: function(element) {
				return $("#name3").val();
							}
					           
                       }
}
});
});
</script>
<style type="text/css">
<!--
	label
{
		display: block;
		color: #FF0000;
		font-style: bold;
}
		-->
</style>
</head>

<body>
<form method="POST" id="registerForm" action="employee_id1.php">
								<table border="1" cellspacing="1"
								 cellpadding="5" width="100%">
								  <tr>
									 <td>
										<table width="100%" cellspacing="5">
										  <tr>

											 <td width="3%">
												&nbsp;</td>

											 <td width="12%">
												employee id</td>

											 <td width="81%">
												name</td>
										  </tr>
										  <tr>

											 <td width="3%">
												1</td>

											 <td width="12%">

												<input name="id1" size="12"></td>

											 <td width="81%">

												<select size="1" value="" name="title1">
												<option selected>
												</option>
												<option value="mr">mr</option>
												<option value="mrs">mrs</option>
												<option value="ms">ms</option>
												<option value="miss">miss
												</option>
												</select><input name="name1" size="12"></td>
										  </tr>
										  <tr>

											 <td width="3%">
												2</td>

											 <td width="12%">

												<input name="id2" size="12"></td>

											 <td width="81%">

												<select size="1" name="title2">
												<option selected>
												</option>
												<option value="mr">mr</option>
												<option value="mrs">mrs</option>
												<option value="ms">ms</option>
												<option value="miss">miss
												</option>
												</select><input name="name2" size="12"></td>
										  </tr>
										  <tr>

											 <td width="3%">
												3</td>

											 <td width="12%">

												<input name="id3" size="12"></td>

											 <td width="81%">

												<select size="1" name="title3">
												<option selected>
												</option>
												<option value="mr">mr</option>
												<option value="mrs">mrs</option>
												<option value="ms">ms</option>
												<option value="miss">miss
												</option>
												</select><input name="name3" size="12"></td>
										  </tr>
										  </table> </td>
								  </tr>
								</table>

<p><input type="submit" value="submit"><input type="reset" value="clear form"> </p>
</form>
</body>

</html>

Open in new window

0
Comment
Question by:bootneck2222
2 Comments
 
LVL 17

Accepted Solution

by:
sonawanekiran earned 500 total points
Comment Utility
check this

<html>

<head>
<meta http-equiv="Content-Language" content="en-gb">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Employee ID</title>
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript" src="jquery.validate.min.js"></script>
<script>
$(document).ready(function(){
        $("#registerForm").validate({
        rules: {
               id1: "required",
               name1: "required",
               title1: "required",
               title2: {
        required: function(element) {
          var flag = false;
          if($("#name2").val() != "")
           {
             if($("#title2").val()=="")
             flag = true;
           } 
           return flag;
              }
          }, 
               title3: {
        required: function(element) {
       var flag = false;
          if($("#name3").val() != "")
           {
             if($("#title3").val()=="")
             flag = true;
           } 
           return flag;
              }
                     
                       }
}
});
});
</script>
<style type="text/css">
<!--
  label
{
    display: block;
    color: #FF0000;
    font-style: bold;
}
    -->
</style>
</head>

<body>
<form method="POST" id="registerForm" action="employee_id1.php">
                <table border="1" cellspacing="1"
                 cellpadding="5" width="100%">
                  <tr>
                   <td>
                    <table width="100%" cellspacing="5">
                      <tr>

                       <td width="3%">
                        &nbsp;</td>

                       <td width="12%">
                        employee id</td>

                       <td width="81%">
                        name</td>
                      </tr>
                      <tr>

                       <td width="3%">
                        1</td>

                       <td width="12%">

                        <input name="id1" id="id1" size="12"></td>

                       <td width="81%">

                        <select size="1"  id="title1" value="" name="title1">
                        <option selected>
                        </option>
                        <option value="mr">mr</option>
                        <option value="mrs">mrs</option>
                        <option value="ms">ms</option>
                        <option value="miss">miss
                        </option>
                        </select><input name="name1"  size="12" id="name1"></td>
                      </tr>
                      <tr>

                       <td width="3%">
                        2</td>

                       <td width="12%">

                        <input name="id2" size="12"  id="id2"></td>

                       <td width="81%">

                        <select size="1" name="title2" id="title2">
                        <option selected>
                        </option>
                        <option value="mr">mr</option>
                        <option value="mrs">mrs</option>
                        <option value="ms">ms</option>
                        <option value="miss">miss
                        </option>
                        </select><input name="name2" size="12"  id="name2"></td>
                      </tr>
                      <tr>

                       <td width="3%">
                        3</td>

                       <td width="12%">

                        <input name="id3" size="12" id="id3"></td>

                       <td width="81%">

                        <select size="1" name="title3"  id="title3">
                        <option selected>
                        </option>
                        <option value="mr">mr</option>
                        <option value="mrs">mrs</option>
                        <option value="ms">ms</option>
                        <option value="miss">miss
                        </option>
                        </select><input name="name3" size="12" id="name3"></td>
                      </tr>
                      </table> </td>
                  </tr>
                </table>

<p><input type="submit" value="submit"><input type="reset" value="clear form"> </p>
</form>
</body>

</html>

Open in new window

0
 

Author Closing Comment

by:bootneck2222
Comment Utility
Excellent. Thank you for the quick response.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

763 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

8 Experts available now in Live!

Get 1:1 Help Now