Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selectize.js and jQuery validation

I'm trying to validate a form that uses the Selectize.js to select and jQuery Validation plugin for validation.

I can not make it work. The script validates the input fields, but not the select:

<form id="test" method="post" action="#">
Name: <input name="name" type="text">

<select name="person" id="select-beast" class="demo-default" placeholder="Select">
    <option value="">Select...</option>
    <option value="1">First</option>
    <option value="2">Second</option>n>
</select>

    <button type="submit" class="btn btn-primary col-md-12 col-lg-12">Go</button>
</form>

JS:

$('#select-beast').selectize({
    create: true,
    sortField: 'text'
});
    

// validate signup form on keyup and submit
$("#test").validate({
    errorElement: 'label',
    errorClass: 'error',
    rules: {
        name: {
            required: true
        },
        person: {
            required: true
        }
    },
    messages: {
        name: "Insert name.",
        person: "Insert person.",
    }
});

http://jsfiddle.net/8nVqS/

like image 471
Ivan Avatar asked Mar 27 '14 19:03

Ivan


People also ask

What is Selectize in jQuery?

Selectize is a jQuery-based custom <select> UI control. Useful for tagging, contact lists, country selectors, etc.

What is jQuery validation used for?

The jQuery Validate is a feature provided by jQuery that makes it easier for the creator to validate the user or visitor's input data while keeping your code free of all the mesh of validation rules from javaScript code.

How we can use jQuery validation plugins in MVC?

For jQuery form validation to work, we set "HtmlHelper. UnobtrusiveJavaScriptEnabled = false;" property false in the register form instead of "web. config" file; this means if we set the value false for above property in "web. config" file, then we will disable client side validation across application.

Does jQuery validate require a form?

The jquery validate plugin requires a form element to function, so you should have your form fields (no matter how few) contained inside a form. You can tell the validation plugin not to operate on form submission, then manually validate the form when the correct submit button is clicked.


1 Answers

The reason is that jQuery.validation plugin will ignore all hidden element when validating by default.

And Selectize.js will hide the given jQuery object, So your name field will run validate but person field will not.

Solution, please reference this gist: How to validate selectize.js comboboxes with the jQuery validation plugin

like image 74
Yang Hailong Avatar answered Oct 06 '22 00:10

Yang Hailong