Last active
June 13, 2024 21:38
-
-
Save yannleretaille/8498983 to your computer and use it in GitHub Desktop.
How to validate selectize.js comboboxes with the jQuery validation plugin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//How to validate selectize.js comboboxes with the jQuery validation plugin | |
//selectize.js: http://brianreavis.github.io/selectize.js/ (brianreavis/selectize.js) | |
//http://jqueryvalidation.org (jzaefferer/jquery-validation) | |
//configure jquery validation | |
$("#commentForm").validate({ | |
//the default ignore selector is ':hidden', the following selectors restore the default behaviour when using selectize.js | |
//:hidden:not([class~=selectized]) | selects all hidden elements, but not the original selects/inputs hidden by selectize | |
//:hidden > .selectized | to restore the behaviour of the default selector, the original selects/inputs are only validated if their parent is visible | |
//.selectize-control .selectize-input input | this rule is not really necessary, but ensures that the temporary inputs created by selectize on the fly are never validated | |
ignore: ':hidden:not([class~=selectized]),:hidden > .selectized, .selectize-control .selectize-input input' | |
//optional: add rules etc. according to jquery-validation docs | |
}); | |
if($("#commentForm").valid()) { | |
alert("this form uses some selectize comboboxes and is valid!"); | |
} |
The answers provided above aren't working with selectize.js v0.12.3 or greater.
That is happening due to the fact that the required attribute is removed from the select in refreshValidityState function - see selectize/selectize.js@abc8a56
A quick fix for this is to add the selects that are required in the rules array of the validation options object.
Example:
var validator = $("#form").validate({
ignore: ':hidden:not([class~=selectized]),:hidden > .selectized, .selectize-control .selectize-input input',
rules: {
"select-name-here": "required",
"select-name-here2": "required"
}
});
Thank you for this!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
hello, as they eliminate the error message validation? when changing the selection?
thx