Solved

jQuery - validating a field based on another field

Posted on 2011-09-27
2
282 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Today I would like to talk about localizing (Internationalization) JavaScript applications. Introduction When creating an application that is going to be used by many people around the globe, it is important to remember that not everyone speak…
Avoid defining the variables in the global scope; trying to define them in a local function scope. Because:   • Look-up is performed every time a variable is accessed.   • Variables are resolved backwards from most specific to least specific scope…
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…

813 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

12 Experts available now in Live!

Get 1:1 Help Now