Solved

jQuery - validating a field based on another field

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

Get HTML5 Certified

Want to be a web developer? You'll need to know HTML. Prepare for HTML5 certification by enrolling in July's Course of the Month! It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

This article discusses the difference between strict equality operator and equality operator in JavaScript. The Need: Because JavaScript performs an implicit type conversion when performing comparisons, we have to take this into account when wri…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
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…
Suggested Courses

617 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