BootstrapValidator

Edit on Github

Introduction

BootstrapValidator is the best jQuery plugin to validate form fields, designed to use with Bootstrap 3. It's developed with love by @nghuuphuoc.

The latest version, v0.4.5, is released on 2014-05-15

<form id="tryitForm" class="form-horizontal">
    <div class="form-group">
        <label class="col-md-3 control-label">Full name</label>
        <div class="col-md-4">
            <input type="text" class="form-control" name="firstName" />
        </div>
        <div class="col-md-4">
            <input type="text" class="form-control" name="lastName" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Email address</label>
        <div class="col-md-6">
            <input type="text" class="form-control" name="email" />
        </div>
    </div>

    <div class="form-group">
        <label class="col-md-3 control-label">Gender</label>
        <div class="col-md-6">
            <div class="radio">
                <label><input type="radio" name="gender" value="male" /> Male</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="gender" value="female" /> Female</label>
            </div>
            <div class="radio">
                <label><input type="radio" name="gender" value="other" /> Other</label>
            </div>
        </div>
    </div>

    <div class="form-group">
        <div class="col-md-offset-3 col-md-8">
            <button type="submit" class="btn btn-primary">Say hello</button>
        </div>
    </div>
</form>
<script>
$(document).ready(function() {
    $('#tryitForm').bootstrapValidator({
        fields: {
            firstName: {
                validators: {
                    notEmpty: {
                        message: 'The first name is required and cannot be empty'
                    }
                }
            },
            lastName: {
                validators: {
                    notEmpty: {
                        message: 'The last name is required and cannot be empty'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: 'The email address is required'
                    },
                    emailAddress: {
                        message: 'The input is not a valid email address'
                    }
                }
            },
            gender: {
                validators: {
                    notEmpty: {
                        message: 'The gender is required'
                    }
                }
            }
        },
        submitHandler: function(validator, form, submitButton) {
            var fullName = [validator.getFieldElements('firstName').val(),
                            validator.getFieldElements('lastName').val()].join(' ');
            $('#helloModal')
                .find('.modal-title').html('Hello ' + fullName).end()
                .modal();
        }
    });
});
</script>

Features

Code

  • Written from scratch
  • Very solid and clean
  • The core plugin and validators code are separated

Bootstrap

HTML 5

  • Support setting the options via HTML 5 attributes prefixed with data-bv-
  • Support HTML 5 attributes such as min, max, pattern, required
  • Support HTML 5 input types such as color, email, range and url

Flexible

  • The elements can be defined by either name or CSS selector
  • Support multiple elements with the same name
  • Possible to indicate excluded fields
  • Field validators can be enabled/disabled on the fly

Richest Validators

Ajax

Friendly

Compatibility

We play nice with all of them!

What's News?

The latest v0.4.5 version comes with following new features and fixes:

  • #245: The cvv validator should support spaces in credit card, thanks to @evilchili
  • Change default submitButtons to [type="submit"] to support input type="submit"
  • #226: Fix the conflict issue with MooTools
  • #238: The submit buttons are not sent
  • #253: The iban validator does not work on IE8
  • #257: Plugin method invocation don't work
  • Fix the issue that the hidden fields generated by other plugins might not be validated
  • When parsing options from HTML attributes, don't add the field which hasn't validators. It improves fixes for #191, #223

Contributors

It is much better by the amazing developers