#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    222
    Rep Power
    11

    Question JQuery Validation Plug-In: Need Help w/ Dropdowns


    I'm trying to use the jQuery Validation plug-in on a form on my site. Two problems -- it doesn't seem to work and I want to use alert boxes for the errors.

    Here's the code:

    Code:
    <script type="text/javascript">
    	jQuery.validator.addMethod(
    		"require_categories",
    		function(value, element) {
    			if (element.value == 0) {
    				return false;
    			} else {
    				return true;
    			},
    			"Please select an option."
    		});
    	$("#publishForm").validate({
    		rules: {
    			category: {
    				require_categories: true
    			}
    		}
    	});
    </script>
    
    <form id="publishForm" method="post">
    <select name="category[]" id="county">
    <option value="">--</option>
    <option value="1">County Name</option>
    ...
    </select>
    </form>
    Any ideas why this isn't working? And how do I configure it to use alert boxes as opposed to inline errors?

    Thank you for your help.
  2. #2
  3. CSS & JS/DOM Adept
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jul 2004
    Location
    USA (verifiably)
    Posts
    20,131
    Rep Power
    4304
    It's probably not working because the form control's name is "category[]", not "category". Neither HTML nor JavaScript give any special treatment to form control names that include a pair of square brackets.

    If you don't have a second form control with that name, remove the square brackets from it. Otherwise, try this:
    Code:
    	$("#publishForm").validate({
    		rules: {
    			'category[]': {
    				require_categories: true
    			}
    		}
    	});
    And how do I configure it to use alert boxes as opposed to inline errors?
    Why would you want to do that? Many people consider using alert boxes to be less user-friendly.
    Spreading knowledge, one newbie at a time.

    Check out my blog. | Learn CSS. | PHP includes | X/HTML Validator | CSS validator | Common CSS Mistakes | Common JS Mistakes

    Remember people spend most of their time on other people's sites (so don't violate web design conventions).

IMN logo majestic logo threadwatch logo seochat tools logo