Search...
FAQs
Subscribe
Pie
FAQs
Recent topics
Flagged topics
Hot topics
Best topics
Search...
Search within HTML Pages with CSS and JavaScript
Search Coderanch
Advance search
Google search
Register / Login
Post Reply
Bookmark Topic
Watch Topic
New Topic
programming forums
Java
Mobile
Certification
Databases
Caching
Books
Engineering
Micro Controllers
OS
Languages
Paradigms
IDEs
Build Tools
Frameworks
Application Servers
Open Source
This Site
Careers
Other
Pie Elite
all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Campbell Ritchie
Tim Cooke
paul wheaton
Jeanne Boyarsky
Ron McLeod
Sheriffs:
Paul Clapham
Liutauras Vilda
Devaka Cooray
Saloon Keepers:
Tim Holloway
Roland Mueller
Bartenders:
Forum:
HTML Pages with CSS and JavaScript
jQuery Validator and select
K Mansoor
Ranch Hand
Posts: 52
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
I'm trying to disallow form submission if SELECT'ed OPTION is 7, however the validation method never gets invoked, the rest of validation works fine.
$(document).ready(function() { jQuery.validator.addMethod( "programid", function(value, element) { if ($('#programid').val() == '7') { return false; } else { return true; } }, "Program cannot be XYZ" ); */ $("#student-form").validate({ rules: { studentNo: { required: true, digits: true }, email: { required: true, email: true }, programid: "required", firstName: "required", lastName: "required" } }); }); HTML Fragment: <form id="student-form" action="/xyz/student" method="POST"> <!-- other controls --> <select id="programid" name="program.id" class="input-field"> <option value="">-- Please select --</option> <option value="3">Dentistry Year 1</option> <option value="4">Med Students Year 1</option> <option value="1">Program 1</option> <option value="7">Program 2</option> </select> </form>
Bear Bibeault
Sheriff
Posts: 67753
173
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
You are using the validation method management incorrectly.
Here's a page I set up that demonstrates how I would go about it:
<!DOCTYPE html> <html> <head> <title>Validation test</title> <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js"></script> <script type="text/javascript" src="scripts/jquery-validation-1.8.1/jquery.validate.min.js"></script> <script type="text/javascript"> $(function() { jQuery.validator.addMethod( "notEqualTo", function(elementValue,element,param) { return elementValue != param; }, "Value cannot be {0}" ); $('#testForm').validate({ errorElement: 'div', rules: { simpleText: { required: true, minlength: 3 }, selector: { required: true, notEqualTo: 7 } }, messages: { simpleText: { required: "You cannot leave simpleText empty!", minlength: "Provide at least {0} characters" }, selector: { required: "Please choose something!" } }, submitHandler: function(){ //Do success stuff here return true; }, errorPlacement: function(error, element) { error.appendTo(element.closest('.item')); }, invalidHandler: function(event,validator) { //do error stuff here return false; } }); }); </script> </head> <body> <form id="testForm" action="/xyz" method="post"> <div class="item"> <label for="simpleTextField">Simple text:</label> <input type="text" id="simpleTextField" name="simpleText"> </div> <div class="item"> <label for="testDropdown">Dropdown:</label> <select id="testDropdown" name="selector"> <option value="">— choose one —</option> <option value="0">Zero</option> <option value="1">One</option> <option value="2">Two</option> <option value="3">Three</option> <option value="7">Seven</option> </select> </div> <div class="item buttonBar"> <button type="submit">OK</button> </div> </form> </body> </html>
Note how much more general it is than your hard-coded approach.
[
Asking smart questions
] [
About Bear
] [
Books by Bear
]
K Mansoor
Ranch Hand
Posts: 52
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
Clean, terse, beautiful!
Thank you.
Bear Bibeault
Sheriff
Posts: 67753
173
I like...
posted 13 years ago
Number of slices to send:
Optional 'thank-you' note:
Send
The validation plugin is really useful, but it can be hard to know how to use its advanced features. I still have a lot to learn about it myself!
Glad this helped get you going!
[
Asking smart questions
] [
About Bear
] [
Books by Bear
]
Right! We're on it! Let's get to work tiny ad!
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
reply
Bookmark Topic
Watch Topic
New Topic
Boost this thread!
Similar Threads
Struts 2 JCaptchaforStruts 2 issue
Two dropdowns - need to indicate to server side code the specific dropdown
Inline Form Validation
Ajax, Struts, and a few pesky event based issues
Javascript Dynamic Dropdown Box validation
More...