Skip to content

Instantly share code, notes, and snippets.

@zerc
Created June 22, 2013 13:28

Revisions

  1. zerc created this gist Jun 22, 2013.
    34 changes: 34 additions & 0 deletions Backbone.Form.Validators.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    (function (Form) {
    Form.validators.errMessages = {
    youtube: 'Enter link from YouTube',
    positive_int: 'Enter positive integer'
    };

    Form.validators.youtube = function(options) {
    options = _.extend({
    type: 'youtube',
    message: this.errMessages.youtube,
    regexp: /^(http|https):\/\/(www\.youtube\.com|youtu.be)\/[a-zA-Z0-9\?&\/=\-]+$/gmi
    }, options);

    return Form.validators.regexp(options);
    };

    Form.validators.positive_int = function(options) {
    options = _.extend({
    type: 'positive_int',
    message: this.errMessages.positive_int
    }, options);

    return function positive_int(value) {
    options.value = value;

    var err = {
    type: options.type,
    message: options.message
    };

    if (value === null || value === undefined || value === false || value === '' || value <= 0) return err;
    };
    };
    }(Backbone.Form));