9951 explained code solutions for 126 technologies


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


Backbone.js provides a simple way to validate user input. You can use the Model.validate() method to check user input and return an error message when the input is invalid. Here is an example of how to use the Model.validate() method:

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

var myModel = new MyModel({name: 'John'});

if (myModel.isValid()) {
  console.log("Valid name");
} else {
  console.log(myModel.validationError);
}

Output example

Name must be at least 5 characters long

The code above checks the length of the name attribute and returns an error message if it is less than 5 characters long. The validate() method takes an attributes argument, which is an object containing the attributes to be validated. The isValid() method returns true if the input is valid, and false if it is not. If the input is not valid, the validationError property will contain the error message.

Code explanation

  1. var MyModel = Backbone.Model.extend({ - This creates a new Backbone Model.
  2. validate: function(attributes) { - This is the validate() method, which takes an attributes argument containing the attributes to be validated.
  3. if (attributes.name.length < 5) { - This checks the length of the name attribute.
  4. return "Name must be at least 5 characters long"; - This is the error message that will be returned if the input is invalid.
  5. var myModel = new MyModel({name: 'John'}); - This creates a new instance of the MyModel model with a name attribute.
  6. if (myModel.isValid()) { - This checks if the input is valid.
  7. console.log(myModel.validationError); - This will display the error message if the input is not valid.

Helpful links

Edit this code on GitHub