Backbone.js: Model Validation Validation data is an important part of any form or data. we should validate data on Client side i.e. browser as well as on Server side. While working on client side Backbone.js, we could use model validation in Backbone.js Model. This Blog gives insight into Model Validation in Backbone.js. You can visit backbone.js doc for more information about models. Suppose we have a Backbone Model for a customer like: var CustomerModel = Backbone.Model.extend({ idAttribute: "id", defaults: { 'name': '', 'email': '', 'phoneNo': '' }, urlRoot: "https://mysite/customer/xhr", }); 123456789 var CustomerModel = Backbone.Model.extend({ idAttribute: "id", defaults: { 'name': '', 'email': '', 'phoneNo': '' }, urlRoot: "https://mysite/customer/xhr",}); If we would validate customerModel then we would not get any error. Since we haven’t added any validation to CustomerModel. customerModel.isValid(); //returns true // when model is binded with current Backbone view, isValid() can be used like // this.model.isValid(); 12345 customerModel.isValid();//returns true // when model is binded with current Backbone view, isValid() can be used like// this.model.isValid(); Now let’s add some validation to CustomerModel Required Validation: var CustomerModel = Backbone.Model.extend({ idAttribute: "id", defaults: { 'name': '', 'email': '', 'phoneNo': '' }, validation: { 'name': { required: true, msg: "enter name" }, 'email': { required: true, msg: "enter email" }, }, urlRoot: "https://mysite/customer/xhr", }); 12345678910111213141516171819 var CustomerModel = Backbone.Model.extend({ idAttribute: "id", defaults: { 'name': '', 'email': '', 'phoneNo': '' }, validation: { 'name': { required: true, msg: "enter name" }, 'email': { required: true, msg: "enter email" }, }, urlRoot: "https://mysite/customer/xhr",}); required keyword is used to add required validation for a field. In similar fashion, we can use other inbuilt validations. Now validating empty customerModel will return false. customerModel.isValid(); //returns false customerModel.set('name', 'my name'); customerMOdel.set('email', 'email@example.com'); customerModel.isValid(); //returns true 1234567 customerModel.isValid();//returns false customerModel.set('name', 'my name');customerMOdel.set('email', '[email protected]');customerModel.isValid();//returns true Pattern Validation: We can also use regex validation in backbone by using pattern keyword. there are some inbuilt pattern like email, alternatively, you can also specify regex pattern without delimiters. ... ... validation: { 'name': [{ required: true, msg: "enter name" }, { pattern: ^([^0-9]*)$, msg: "provide a valid name", }], 'email': [{ required: true, msg: "enter email" }, { pattern: "email", msg: "provide a valid email", }], }, ... ... 12345678910111213141516171819202122 ... ... validation: { 'name': [{ required: true, msg: "enter name" }, { pattern: ^([^0-9]*)$, msg: "provide a valid name", }], 'email': [{ required: true, msg: "enter email" }, { pattern: "email", msg: "provide a valid email", }], }, ... ... There are also other inbuilt validations like length, minLength, maxLength, min, max, range, oneOf, equalTo. Extend validation callback: if instead of only checking for if model is valid or invalid, we can also extend Backbone validation callbacks for displaying an appropriate message. _.extend(Backbone.Validation.callbacks, { valid: function(view, attr, selector) { // no error for field , do something like remove error from invalid field }, invalid: function(view, attr, error, selector) { //print error or append error to invalid field alert.log('error for field ' + 'attr' + ':' + error); } }); 123456789 _.extend(Backbone.Validation.callbacks, { valid: function(view, attr, selector) { // no error for field , do something like remove error from invalid field }, invalid: function(view, attr, error, selector) { //print error or append error to invalid field alert.log('error for field ' + 'attr' + ':' + error); }}); Now, calling customerModel.isValid() will alert error(s), if model is invalid. What’s More? I found these links useful: https://github.com/thedersen/backbone.validation https://www.tutorialspoint.com/backbonejs/backbonejs_model.htm Tag(s) backbone.js model validation