9951 explained code solutions for 126 technologies


backbone.jsHow can I use Backbone.js to validate user input?


Backbone.js provides a set of validation methods to help you validate user input. It includes basic validation methods such as validate and validateAll which can be used to validate the model data.

For example, you can use the validate method in your model to check if the user input is valid:

var User = Backbone.Model.extend({
  validate: function(attrs) {
    if (attrs.name.length < 5) {
      return "Name must be at least 5 characters long";
    }
  }
});

In the above example, the validate method checks if the name attribute of the model is at least 5 characters long. If it is not, the method returns a string with an error message.

You can also use the validateAll method to validate all the attributes in the model at once. For example:

var User = Backbone.Model.extend({
  validateAll: function(attrs) {
    if (attrs.name.length < 5 && attrs.age < 18) {
      return "Name must be at least 5 characters long and age must be 18 or older";
    }
  }
});

In the above example, the validateAll method checks if the name attribute of the model is at least 5 characters long and if the age is 18 or older. If both conditions are not met, the method returns a string with an error message.

You can also use the on method to add custom validation logic to your models. For example:

var User = Backbone.Model.extend({
  on: {
    'invalid': function(model, error) {
      alert(error);
    }
  }
});

In the above example, the on method adds a custom validation logic which will be triggered when the model is invalid. The on method takes two arguments, the first one is the model and the second one is the error message. The alert method will be triggered when the model is invalid and it will display the error message.

You can also use the isValid method to check if the model is valid or not. For example:

if (user.isValid()) {
  // do something
}

In the above example, the isValid method checks if the model is valid or not. If it is valid, the code inside the if statement will be executed.

Helpful links

Edit this code on GitHub