Solved

jQuery - validating a field based on another field

Posted on 2011-09-27
2
284 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
ID: 36708246
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
ID: 36708408
Excellent. Thank you for the quick response.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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…

830 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