{"id":1410,"date":"2022-08-30T15:16:18","date_gmt":"2022-08-30T15:16:18","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/15\/validating-select-box-with-jquery-validate-and-chosen-plugins-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:16:18","modified_gmt":"2022-08-30T15:16:18","slug":"validating-select-box-with-jquery-validate-and-chosen-plugins-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/validating-select-box-with-jquery-validate-and-chosen-plugins-collection-of-common-programming-errors\/","title":{"rendered":"Validating select box with jquery validate and chosen plugins-Collection of common programming errors"},"content":{"rendered":"<p>Try something like this instead.<\/p>\n<pre><code>$(document).ready(function() {\n\n    \/\/ initialize the plugin on DOM ready\n    $(\"#producteditform\").validate({ \n        \/\/ all rules and options,\n        rules: {\n            nameedit: {\n                required: true,\n                maxlength:100\n            },\n            unitedit:{\n                required: true,\n            }\n        }\n    });\n\n    \/\/ test for valid form\n    $('#save').on('click' function() {\n        if($(\"#producteditform\").valid()) {\n            \/\/ do this on valid form\n        } else {\n            \/\/ do this on invalid form\n        }\n    });\n\n});\n<\/code><\/pre>\n<p><code>$('#producteditform').validate()<\/code> initializes the validate plugin on the <code>form<\/code>.<\/p>\n<p><code>$('#producteditform').valid()<\/code> programmatically triggers an actual validation test and returns a <code>true\/false<\/code> boolean value.<\/p>\n<p>Alternatively, since your <code>event<\/code> is the form <code>click<\/code> on <code>submit<\/code> event, then you simply use the built-in <code>submitHandler<\/code> callback function instead. (There is no need to create another <code>click<\/code> handler.)<\/p>\n<pre><code>$(document).ready(function() {\n\n    \/\/ initialize the plugin on DOM ready\n    $(\"#producteditform\").validate({ \n        \/\/ all rules and options,\n        rules: {\n            nameedit: {\n                required: true,\n                maxlength:100\n            },\n            unitedit:{\n                required: true,\n            }\n        },\n        submitHandler: function(form) {\n            \/\/ do this on valid form\n        }\n    });\n\n});\n<\/code><\/pre>\n<p><strong>Full documentation:<\/strong><\/p>\n<p>http:\/\/docs.jquery.com\/Plugins\/Validation<\/p>\n<p>Until you show your HTML output instead of the PHP, here is a generic example of how to validate a <code>select<\/code> element:<\/p>\n<p><strong>http:\/\/jsfiddle.net\/6Dt7F\/<\/strong><\/p>\n<p>Important, the very first, or the default <code>option<\/code> in the <code>select<\/code> element must contain <code>value=\"\"<\/code> or the <code>required<\/code> rule will fail.<\/p>\n<pre><code>\n    please choose...\n    option 1\n    option 2\n    option 3\n\n<\/code><\/pre>\n<p><strong>EDIT<\/strong>:<\/p>\n<p>I still don&#8217;t have enough code to make a working demo. However, since the <code>chosen<\/code> plugin is hiding the actual form elements, you&#8217;ll have to do the following&#8230;<\/p>\n<p>1) Target the actual form element that is going to get validated and pass the data, not the visual element created by <code>chosen<\/code>.<\/p>\n<p>2) Enable jQuery Validate to work on hidden elements by setting <code>ignore: []<\/code> option.<\/p>\n<p>Try this&#8230;<\/p>\n<pre><code>$(document).ready(function () {\n\n    \/\/ initialize the plugin on DOM ready\n    $(\"#producteditform\").validate({ \n        \/\/ all rules and options,\n        ignore: [], \/\/<\/code><\/pre>\n<p id=\"rop\"><small>Originally posted 2013-11-15 09:10:35. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>Try something like this instead. $(document).ready(function() { \/\/ initialize the plugin on DOM ready $(&#8220;#producteditform&#8221;).validate({ \/\/ all rules and options, rules: { nameedit: { required: true, maxlength:100 }, unitedit:{ required: true, } } }); \/\/ test for valid form $(&#8216;#save&#8217;).on(&#8216;click&#8217; function() { if($(&#8220;#producteditform&#8221;).valid()) { \/\/ do this on valid form } else { \/\/ do [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1410","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1410","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1410"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1410\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1410"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1410"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1410"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}