Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use jQuery validation with the "chosen" plugin?

I have some <select> inputs using the chosen plugin that I want to validate as "required" on the client side. Since "chosen" hides the actual select element and creates a widget with divs and spans, native HTML5 validation doesn't seem to work properly. The form won't submit (which is good), but the error message is not shown, so the user has no idea what's wrong (which is not good).

I've turned to the jQuery validation plugin (which I planned on using eventually anyways) but haven't had any luck so far. Here's my test case:

<form>     <label>Name: <input name="test1" required></label>     <label>Favorite Color:         <select name="test2" required>             <option value=""></option>             <option value="red">Red</option>             <option value="blue">Blue</option>             <option value="green">Green</option>         </select>     </label>     <input type="submit"> </form> 
$(document).ready(function(){     $('select').chosen();     $('form').validate(); }); 

This is letting the select through with an empty value, without validating or showing the error message. When I comment out the chosen() line, it works fine.

How can I validate chosen() inputs with the jQuery validation plugin, and show the error message for invalid ones?

like image 699
Wesley Murch Avatar asked Jun 27 '12 18:06

Wesley Murch


People also ask

How we can use jQuery validation plugins in MVC?

The jQuery validation plugin leverages a CSS selector like syntax to apply a set of validation rules. You can download the plugin (js) file from jQuery website. The password and confirm password objects are matched by the validation plugin for you and shows the message on the equalTo attribute if they don't match.

How do you validate a date field with jQuery validation plugin?

Simple JavaScript Function to to check if date is valid.getMonth() + 1) == bits[1] && d. getDate() == Number(bits[0])); } //Test it! var currentDate = new Date('31/09/2011'); console.

How use jQuery remote validation?

rules: { username: { minlength: 6, maxlength: 12, remote: { url: 'register/isUsernameAvailable', dataType: 'post', data: { 'username': $('#username'). val() }, success: function(data) { if (data. username == 'found') { message: { username: 'The username is already in use!


2 Answers

jQuery validate ignores the hidden element, and since the Chosen plugin adds visibility:hidden attribute to the select, try:

$.validator.setDefaults({ ignore: ":hidden:not(select)" }) //for all select

OR

$.validator.setDefaults({ ignore: ":hidden:not(.chosen-select)" }) //for all select having class .chosen-select

Add this line just before validate() function. It works fine for me.

like image 142
Anupal Avatar answered Sep 20 '22 18:09

Anupal


jQuery validation isn't going to pick up elements that are hidden, but you can force it to validate individual elements. Bit of a hack, but the following will work:

$('form').on('submit', function(e) {     if(!$('[name="test2"]').valid()) {         e.preventDefault();     } });   

To select only "chosen" elements you can use $('.chzn-done')

like image 37
Mike Robinson Avatar answered Sep 20 '22 18:09

Mike Robinson