--- title: Form Validation nav_title: Form Validation tags: demo ---

User input validation in Tom Select is built on standard validation protocols introduced by HTML5. As a user changes the items of a Tom Select instance, values are updated on the original <input> or <select> element where validity is then determined. Validation criteria can be defined on the original <input> using required, pattern, type, and other standard validation attributes.

To communicate the validation state of an input, Tom Select again gracefully falls back on the default browser functionality. You can also enhance your communication with custom styles and messages with a few lines of JavaScript.

{% from "demo.njk" import demo %} {% set label %}

Browsers will prevent users from submitting this form unless the <select required> field has a value. Your browser may display a message along the lines of "Please fill out this field" when trying to submit the form.

{% endset %} {% set html %}
{% endset %} {{ demo( label, html, script) }} {% set label %}

With a few changes, you can enhance your communication with features provided by frameworks like Bootstrap.

For custom Bootstrap form validation messages, you’ll need to add the novalidate boolean attribute to your <form>. This disables the browser default feedback tooltips, but still provides access to the form validation APIs in JavaScript.

Then, within your submit listener, you can either add the was-validated class to the form or toggle the is-invalid class on the Tom Select wrapper.

{% endset %} {% set html %}
Please select a person
{% endset %} {{ demo( label, html, script) }} {% set label %}

Tom Select works with validation attributes other than just the required attribute.

{% endset %} {% set html %}
{% endset %} {{ demo( label, html, script) }}