forked from thedersen/backbone.validation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
backbone.validation.min.js
8 lines (8 loc) · 5.75 KB
/
backbone.validation.min.js
1
2
3
4
5
6
7
8
// Backbone.Validation v0.4.0
//
// Copyright (C)2011 Thomas Pedersen
// Distributed under MIT License
//
// Documentation and full license availabe at:
// http://github.com/thedersen/backbone.validation
var isServer=typeof require!=="undefined";if(isServer){var Backbone=require("backbone"),_=require("underscore")._}Backbone.Validation=function(a,b,c){var d={forceUpdate:false,selector:"name"};var e=function(a){return b.reduce(b.keys(a.validation),function(a,b){a[b]=c;return a},{})};var f=function(c,d){var e=c.validation[d]||{};if(b.isFunction(e)){return e}else if(b.isString(e)){return c[e]}else if(!b.isArray(e)){e=[e]}return b.reduce(e,function(c,d){b.each(b.without(b.keys(d),"msg"),function(b){c.push({fn:a.Validation.validators[b],val:d[b],msg:d.msg})});return c},[])};var g=function(a,c,d){var e=f(a,c);if(b.isFunction(e)){return e.call(a,d,c)}return b.reduce(e,function(b,e){var f=e.fn(d,c,e.val,a);if(f===false||b===false){return false}if(f&&!b){return e.msg||f}return b},"")};return{bind:function(a){a.validate=function(c){if(!a.validation){console.log("no validation found in model ",a);return false}var d=[],e=[];for(var f in c){var h=g(a,f,c[f]);if(h){d.push(h);e.push(f)}}return b.isEmpty(d)?false:d}}}}(Backbone,_);Backbone.Validation.OldModel=Backbone.Model;Backbone.Validation.Model=Backbone.Model.extend({initialize:function(){Backbone.Validation.bind(this);Backbone.Validation.OldModel.prototype.initialize.call(this)}});Backbone.Model=Backbone.Validation.Model;Backbone.Validation.patterns={digits:/^\d+$/,number:/^-?(?:\d+|\d{1,3}(?:,\d{3})+)(?:\.\d+)?$/,email:/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i,url:/^(https?|ftp):\/\/(((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:)*@)?(((\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5])\.(\d|[1-9]\d|1\d\d|2[0-4]\d|25[0-5]))|((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?)(:\d*)?)(\/((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)+(\/(([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)*)*)?)?(\?((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?$/i};Backbone.Validation.messages={required:"{0} is required",acceptance:"{0} must be accepted",min:"{0} must be grater than or equal to {1}",max:"{0} must be less than or equal to {1}",range:"{0} must be between {1} and {2}",length:"{0} must be {1} characters",minLength:"{0} must be at least {1} characters",maxLength:"{0} must be at most {1} characters",rangeLength:"{0} must be between {1} and {2} characters",oneOf:"{0} must be one of: {1}",equalTo:"{0} must be the same as {1}",pattern:"{0} must be a valid {1}"};Backbone.Validation.validators=function(a,b,c){var d=String.prototype.trim?function(a){return a===null?"":String.prototype.trim.call(a)}:function(a){var b=/^\s+/,c=/\s+$/;return a===null?"":a.toString().replace(b,"").replace(c,"")};var e=function(){var a=Array.prototype.slice.call(arguments);var b=a.shift();return b.replace(/\{(\d+)\}/g,function(b,c){return typeof a[c]!="undefined"?a[c]:b})};var f=function(b){return c.isNumber(b)||c.isString(b)&&b.match(a.number)};var g=function(a){return!(c.isNull(a)||c.isUndefined(a)||c.isString(a)&&d(a)==="")};return{fn:function(a,b,d,e){if(c.isString(d)){d=e[d]}return d.call(e,a,b)},required:function(a,d,f,h){var i=c.isFunction(f)?f.call(h):f;if(!i&&!g(a)){return false}if(i&&!g(a)){return e(b.required,d)}},acceptance:function(a,d){if(a!=="true"&&(!c.isBoolean(a)||a===false)){return e(b.acceptance,d)}},min:function(a,c,d){if(!f(a)||a<d){return e(b.min,c,d)}},max:function(a,c,d){if(!f(a)||a>d){return e(b.max,c,d)}},range:function(a,c,d){if(!f(a)||a<d[0]||a>d[1]){return e(b.range,c,d[0],d[1])}},length:function(a,c,f){if(!g(a)||d(a).length!==f){return e(b.length,c,f)}},minLength:function(a,c,f){if(!g(a)||d(a).length<f){return e(b.minLength,c,f)}},maxLength:function(a,c,f){if(!g(a)||d(a).length>f){return e(b.maxLength,c,f)}},rangeLength:function(a,c,f){if(!g(a)||d(a).length<f[0]||d(a).length>f[1]){return e(b.rangeLength,c,f[0],f[1])}},oneOf:function(a,d,f){if(!c.include(f,a)){return e(b.oneOf,d,f.join(", "))}},equalTo:function(a,c,d,f){if(a!==f.get(d)){return e(b.equalTo,c,d)}},pattern:function(c,d,f){if(!g(c)||!c.toString().match(a[f]||f)){return e(b.pattern,d,f)}}}}(Backbone.Validation.patterns,Backbone.Validation.messages,_)