Skip to content

Instantly share code, notes, and snippets.

@zerc
Created June 22, 2013 13:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zerc/5840864 to your computer and use it in GitHub Desktop.
Save zerc/5840864 to your computer and use it in GitHub Desktop.
Some addition validators for Backbone.Form
(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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment