@import url('http://getbootstrap.com/dist/css/bootstrap.css');
form {
padding: 0 20px;
}
JavaScript
// Since we are automatically updating the model, we want the model
// to also hold invalid values, otherwise, we might be validating
// something else than the user has entered in the form.
// See: http://thedersen.com/projects/backbone-validation/#configuration/force-update
Backbone.Validation.configure({
forceUpdate: true
});
// Extend the callbacks to work with Bootstrap, as used in this example
// See: http://thedersen.com/projects/backbone-validation/#configuration/callbacks
_.extend(Backbone.Validation.callbacks, {
valid: function (view, attr, selector) {
var $el = view.$('[name=' + attr + ']'),
$group = $el.closest('.form-group');
$group.removeClass('has-error');
$group.find('.help-block').html('').addClass('hidden');
},
invalid: function (view, attr, error, selector) {
var $el = view.$('[name=' + attr + ']'),
$group = $el.closest('.form-group');
$group.addClass('has-error');
$group.find('.help-block').html(error).removeClass('hidden');
}
});
// Define a model with some validation rules
var SignUpModel = Backbone.Model.extend({
defaults: {
country: 'Norway'
},
validation: {
username: {
required: true
},
email: {
required: true,
pattern: 'email'
},
password: {
minLength: 8
},
repeatPassword: {
equalTo: 'password',
msg: 'The passwords does not match'
},
country: {
oneOf: ['Norway', 'Sweeden']
},
gender: {
required: true
},
age: {
required: false,
range: [18, 100]
},
terms: {
acceptance: true
}
}
});
var SignUpForm = Backbone.View.extend({
events: {
'click #signUpButton': function (e) {
e.preventDefault();
this.signUp();
}
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.