Category Archives: backboneJS

Backbone.Validation

Update: If you are looking for documentation for Backbone.Validation it can be foundhere

I just released version 0.2.0 of my validation plugin for Backbone.js. The plugin lets you declare validation rules on your Backbone model, ensuring that input from the user is valid. It currently ships with 18 built-in validators such as required, min, max, range, length, minLength, maxLength, regex-validator etc. In addition to the built-in validators, it is very easy to extend with your custom validators.

Lets start with an example of how to declare validation rules on your model:

 1 var SomeModel = Backbone.Model.extend({
 2   validation: {
 3     name: {
 4       required: true,
 5       msg: 'Name is required'
 6     },
 7     age: {
 8       range: [1, 80]
 9     },
10     email: {
11       pattern: 'email'
12     },
13     someAttribute: function(value) {
14       if(value !== 'somevalue') {
15         return 'Error';
16       }
17     }
18   }
19 }); Continue reading