Avatar of bootneck2222
bootneck2222

asked on 

Validate a field, based on a user dropdown selection

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.
JavaScript

Avatar of undefined
Last Comment
bootneck2222
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

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>
ASKER CERTIFIED SOLUTION
Avatar of chaitu chaitu
chaitu chaitu
Flag of India image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Rikin Shah
Rikin Shah
Flag of India image

Hi,

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

ASKER

Many thanks  chaituu
JavaScript
JavaScript

JavaScript is a dynamic, object-based language commonly used for client-side scripting in web browsers. Recently, server side JavaScript frameworks have also emerged. JavaScript runs on nearly every operating system and in almost every mainstream web browser.

127K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo