Backbone.js: Model Validation

Updated 16 December 2021

Facebook Linkedin

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:

If we would validate customerModel then we would not get any error. Since we haven’t added any validation to CustomerModel.

Now let’s add some validation to CustomerModel

Required Validation:

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.

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.

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.

Now, calling customerModel.isValid() will alert error(s), if model is invalid.

What’s More?

I found these links useful:



. . .

Leave a Comment

Your email address will not be published. Required fields are marked*

Be the first to comment.