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

# Validator validator.js

## Examples

Add validation to your forms with this simple plugin.

Name
Twitter
@
Hey look, this one has feedback icons!
Email
Password
Minimum of 6 characters
Boxers
Briefs
Check yourself
{% highlight html %}
Name
Twitter
@
Hey look, this one has feedback icons!
Email
Password
Minimum of 6 characters
Boxers
Briefs
Check yourself
{% endhighlight %}

## Usage

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 %}

### Markup

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:

- `type="email"`
- `type="url"`
- `type="number"`, with additional constraints via `max` and `min` attributes
- `pattern="Reg(ular )?Exp(ression)?"` (for input types of `text`, `search`, `tel`, `url` or `email`)
- `required`

As well as the following non-standard attributes:

- `data-match="#inputToMatch"` to ensure two fields match, e.g. password confirmations
- `data-minlength="5"` to enforce a minimum amount of characters
- `data-remote="/path/to/remote/validator"` to make an AJAX request to determine if the field is valid or not. Be sure to give the input a `name` attribute, as the request will be sent to `/path/to/remote/validator?<name>=<value>`. The remote endpoint should return a `200 OK` if the field is valid, and a `4xx` otherwise. Here's a reference server implementation using Express.

#### 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 %}
Email
{% endhighlight %}

### Options

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.
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 a truthy or falsy value based on the validity of the input. Object structure is: `{foo: function($el) { return true || false } }` Adding the validator to an input is done just like the others, `data-foo="bar"`. You must also add default error messages for any custom validators via the `errors` option.
errors | object | sensible defaults | Error messages to show for each type of validation. Defaults to: {% highlight js %} errors: { match: 'Does not match', minlength: 'Not long enough' } {% endhighlight %}

#### 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-match-error=""` or use `data-error=""` for a blanket error message to show for any errors on that field.

### Methods

#### $().validator(options)

Attaches a validator to a form collection.

#### $().validator('validate')

Immediately validates the entire form.

#### $().validator('destroy')

Destroys form validator and cleans up data left behind.

### Events

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 %}