Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

jQuery - validating a field based on another field

Posted on 2011-09-27
2
Medium Priority
?
289 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 2000 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

The task A number given should be formatted for easy reading by separating digits into triads. Format must be made inline via JavaScript, i.e., frameworks / functions are not welcome. So let’s take a number like this “12345678.91¿ and format i…
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…

722 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