Link to home
Start Free TrialLog in
Avatar of EMB01
EMB01Flag for United States of America

asked on

jQuery Validation Plugin - Only validate form that's showing...

In the attached script there's a form with a dropdown box and two sections.  When they select "option one," section one should display (section one should display automatically).  If the select "option two," it should display.  I am using the jQuery validation plugin (http://bassistance.de/jquery-plugins/jquery-plugin-validation/) and I don't want it to validate the fields that aren't showing.  I only want it to validate the fields that are currently displayed according the the selection the user made.
<script type="text/javascript">

$().ready(function() {
	
	// validate signup form on keyup and submit
	$("#signupForm").validate({
		rules: {
			firstname: "required",
			lastname: "required",
			username: {
				required: true,
				minlength: 2
			}
		},
		messages: {
			firstname: "Please enter your firstname",
			lastname: "Please enter your lastname",
			username: {
				required: "Please enter a username",
				minlength: "Your username must consist of at least 2 characters"
			},
		}
	});

});

</script>
<form action="" method="post">

<!-- 
WHEN THE USER HITS OPTION ONE OR TWO, IT WILL REVEAL THE CORRESPONDING FORM FIELDS.  I ONLY WANT THE VALIDATION PLUGIN TO VALIDATE THE FIELDS THAT ARE DISPLAYING.  I DON'T WANT IT TO TRY TO VALIDATE THE FIELDS THAT AREN'T SHOWING. 
-->

<select>
  <option value="volvo">Option one</option>
  <option value="saab">Option two</option>
</select>

<!-- BEGIN OPTION ONE -->

		<p>
			<label for="firstname">Firstname</label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>

<!-- END OPTION ONE -->

<!-- BEGIN OPTION TWO -->

		<p>
			<label for="firstname">Firstname</label>
			<input id="firstname" name="firstname" />
		</p>
		<p>
			<label for="lastname">Lastname</label>
			<input id="lastname" name="lastname" />
		</p>

<!-- END OPTION TWO -->

</form>

Open in new window

Avatar of ccwill88
ccwill88
Flag of Taiwan, Province of China image

You can add "disabled " attribute in your tag.

<input style="display:none" disabled type='text' name='test1' />

Open in new window

Another way:
You can add
ignore:":hidden",

Open in new window

in your validate setting.

Like this:

$("#signupForm").validate({
                ignore:":hidden",
		rules: {
			firstname: "required",
			lastname: "required",
			username: {
				required: true,
				minlength: 2
			}
		},...

Open in new window

Avatar of EMB01

ASKER

So, how do I make the dropdown box display and hide the correct section?
ASKER CERTIFIED SOLUTION
Avatar of ccwill88
ccwill88
Flag of Taiwan, Province of China image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial