EMB01
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>
Another way:
You can add
Like this:
You can add
ignore:":hidden",
in your validate setting.Like this:
$("#signupForm").validate({
ignore:":hidden",
rules: {
firstname: "required",
lastname: "required",
username: {
required: true,
minlength: 2
}
},...
ASKER
So, how do I make the dropdown box display and hide the correct section?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Open in new window