Solved

Validate a field, based on a user dropdown selection

Posted on 2013-12-05
4
239 Views
Last Modified: 2013-12-05
Hi,

I would like the form below to validate the colour2 field, if the colour field is set to other. At present its not working. The contents of the validate2.js script is also below:

Form:

<html>

<head>
<script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
<script type="text/javascript" src="js/jquery.validate.min.js"></script>
<script type="text/javascript" src="js/validate2.js"></script>
<title>Registration Form</title>

<style type="text/css">
<!--
	label
{
		display: block;
		color: #FF0000;
		font-style: bold;
}
		-->
</style>
</head>

<body>
<form method="POST" id="registerForm" action="next_stage.php">

<select size="1" name="colour">
						<option selected value="">Select....</option>
						<option value="blue">Blue</option>
						<option value="red">Red</option>
						<option value="other">Other</option>
						</select>
					<input type="text" name="colour2" id="colour2"/>

<input type="submit" value="Submit" name="B1">
</form>
</body>

</html>

validate2.js:

$(document).ready(function(){
        $("#registerForm").validate({
        rules: {
               colour: "required",
               colour2: {
	               required: function(element) {
	              var flag = false;
	                 if($("#colour").val()=="other")
	                  {
	                    if($("#colour2").val()=="")
	                    flag = true;
	                  } 
	                  return flag;
	                     }
	                            
                       }

}
});
});

Open in new window


Any help would be greatly appreciated.
0
Comment
Question by:bootneck2222
  • 2
4 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
Comment Utility
define id for select box

<select size="1" name="colour"    id="colour">
                                    <option selected value="">Select....</option>
                                    <option value="blue">Blue</option>
                                    <option value="red">Red</option>
                                    <option value="other">Other</option>
                                    </select>
0
 
LVL 20

Accepted Solution

by:
chaitu chaitu earned 300 total points
Comment Utility
after define the id for select box then you can call like this.
<select size="1" name="colour"    id="colour">

if($("#colour").val()=="other")
0
 
LVL 19

Expert Comment

by:Rikin Shah
Comment Utility
Hi,

You have set the function to be called when the document is ready. Set it to index change event of colour and check.
0
 

Author Closing Comment

by:bootneck2222
Comment Utility
Many thanks  chaituu
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

Suggested Solutions

Title # Comments Views Activity
JDeveloper 12c for 32 bit 4 33
How is NULL an Object in Javascript? 4 25
using web browser with BING 40 82
Asp.net mvc 5 5 13
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.
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

12 Experts available now in Live!

Get 1:1 Help Now