--- layout: default title: Validator slug: validator lead: "A simple and user-friendly form validator plugin for Bootstrap 3" ---


This plugin adheres to the conventions set forth by Bootstrap's core jQuery plugins, so be sure to check those out to get a better understanding of the goals and design of this plugin.


Add validation to your forms with this simple plugin.

Hey look, this one has feedback icons!
Minimum of 6 characters
{% highlight html %}
Hey look, this one has feedback icons!
Minimum of 6 characters
{% endhighlight %}


Form validation can be enabled in markup via the data-api or via JavaScript. Automatically enable form validation by adding data-toggle="validator" to your form element. {% highlight html %}
{% endhighlight %} Or activate validation via JavaScript: {% highlight js %} $('#myForm').validator() {% endhighlight %}


Follow Bootstrap's examples for appropriate form markup. It's important that each input field is in its own individual .form-group container for error messages to appear properly.

Validation rules are specified on form inputs via the following standard HTML5 attributes:

As well as the following non-standard attributes:

Standard Attribute Validators

The validation rules for standard HTML5 attributes are handled entirely by the browser using the HTML5 Constraint Validation API. As such, this plugin isn't in control of things like what qualifies as a valid email address or URL. If you find you need more restrictive validations for these fields, you can use the pattern attribute to further constrain what's acceptable.

Be careful that you aren't too restrictive though, which might lead to false negatives and a poor user experience. For example, you'd be surprised at what kinds of email addresses are considered valid according to standards.

Cross-browser Compatibility

Because this plugin depends on the HTML5 Constraint Validation API, Internet Explorer 9 and older are not supported. If you need to support these browsers, you must add a polyfill like Ryan Seddon's H5F.

To display error messages, include a container after the input field with both help-block and with-errors classes.

{% highlight html %}
{% endhighlight %}

Validated fields

By default, the validator will only validate fields that are present when the plugin is initialized. If your form has a dynamic set of fields, you will need to call $(...).validator('update') to inform the plugin that the set of fields to be validated has changed.

The default selector used to determine which fields are validated is:

{% highlight js %} $.fn.validator.Constructor.INPUT_SELECTOR = ':input:not([type="hidden"], [type="submit"], [type="reset"], button)' {% endhighlight %}

You can override this value from within your code if you need to change this default behavior. Alternatively, you can add data-validate="true" / data-validate="false" to a specific input to force its inclusion / exclusion in the set of validated fields.


Options can be passed via data attributes or JavaScript. For data attributes, append the option name to data-, as in data-delay="".

Name type default description
delay number 500 Number of milliseconds to wait before displaying an error on a form field.
html boolean false Insert HTML into the error messages. If false, jQuery's text method will be used to insert content into the DOM. Use text if you're worried about XSS attacks.
disable boolean true Disable the submit button until the form is valid and all required fields are complete.
focus boolean true

Scroll to and focus the first field with an error upon validation of the form.

If, for example, you have a fixed navbar at the top of your page and you need to adjust the amount of padding between the top of the window and the focused field, you can override the following variable:

{% highlight js %} $.fn.validator.Constructor.FOCUS_OFFSET {% endhighlight %}

It defaults to 20 (px).

feedback object glyphicon classes

Override the classes used for form feedback icons. Defaults to Bootstrap's glyphicons:

{% highlight js %} feedback: { success: 'glyphicon-ok', error: 'glyphicon-remove' } {% endhighlight %}
custom object {}

Add custom validators to be run. Validators should be functions that receive the jQuery element as an argument and return an error message if the field is invalid.

Here's an example of a custom validator that checks that an input is equal to some specified value:

{% highlight js %} custom: { equals: function($el) { var matchValue = $el.data("equals") // foo if ($el.val() !== matchValue) { return "Hey, that's not valid! It's gotta be " + matchValue } } } {% endhighlight %}

Adding the validator to an input is done just like the others, by referencing its name as a data attribute: <input data-equals="foo">. In this case, the field will display an error if the user enters anything other than foo.

Error messages for individual form fields

Error messages for individual form fields can alternatively be specified through the use of data attributes. You can specify an error message for each type of validator on a field, i.e. data-pattern-error="", data-required-error="", data-match-error="", etc... or use data-error="" for a blanket error message to show for any errors on that field.



Attaches a validator to a form collection.


Updates the collection of inputs that will be validated. Call this method if the set of fields which need to be validated has changed.


Immediately validates the entire form.


Destroys form validator and cleans up data left behind.


All events are fired on the form element and provide a reference to the form field to which the event pertains via event.relatedTarget.

Event Type Description
validate.bs.validator This event fires immediately when a form field is validated.
invalid.bs.validator This event is fired when a form field becomes invalid. Field errors are provided via event.detail.
valid.bs.validator This event is fired when a form field becomes valid. Previous field errors are provided via event.detail.
validated.bs.validator This event is fired after a form field has been validated.

Conditionally handling the submit event

When the form is invalid, .preventDefault() is called on the submit event. As a result, if you want to hook into the submit event and do something conditionally based on whether or not the form was valid, you can check if the event .isDefaultPrevented(). Be sure your submit handler is bound after the plugin has been initialized on your form.

{% highlight js %} $('#form').validator().on('submit', function (e) { if (e.isDefaultPrevented()) { // handle the invalid form... } else { // everything looks good! } }) {% endhighlight %}