/*! * Guards JavaScript jQuery Plugin v1.1.0 * https://github.com/on-site/guards.js * * Copyright 2010-2013, On-Site.com, http://www.on-site.com/ * Licensed under the MIT license. * * Includes code for email and phone number validation from the jQuery * Validation plugin. http://docs.jquery.com/Plugins/Validation * * Date: Fri Jun 28 00:51:31 2013 -0700 */ (function(e){"use strict";e.guard=function(t){return e.guards.add(t)},e.guard.version="1.1.0",e.Guards=function(){var t=this;this._guards=[],this.named={},this.options={stackErrors:!1},this.constants={notChecked:""},this.defaults={grouped:!1,guard:"required",invalidClass:"invalid-field",liveCallback:function(n){var r=e(n.target);if(!r.is(":guardable"))return;r.clearErrors(),t.applyGuards(function(e){return e.isGrouped()?e.appliesTo(r)?t.parentContext(r).find(":guardable"):!1:r})},messageClass:"error-message",messages:{"undefined":"Please fix this field."},style:{field:{"background-color":"#ffff66"},message:{color:"#ff0000","margin-left":"10px"}},submitCallback:function(){return t.guard(e(this))},tag:"span",target:function(t){var n=e(this).filter(":last");n.is(":radio,:checkbox")&&n[0].nextSibling&&(n=e(n[0].nextSibling));var r=n.next();while(r.size()>0&&r[0].isGuardError)n=r,r=n.next();return t.insertAfter(n),!1}},this.name("allow").using(this.aggregate(this.isAllValid,this.isAllowed)).message(this.arrayMessage("Please enter one of: #{0}.")),this.name("always").using(this.aggregate(this.isAllValid,this.always)).message("There was an error."),this.name("different").grouped().using(this.aggregate(this.passThrough,this.isDifferent)).message("These values must all be different."),this.name("disallow").using(this.aggregate(this.isAllValid,this.isDisallowed)).message(this.arrayMessage("Please don't enter: #{0}.")),this.name("dateUS").using(this.aggregate(this.isAllValid,this.isValidDateUS)).message("Please use: dd/mm/yyyy."),this.name("timeUS").using(this.aggregate(this.isAllValid,this.isValidTimeUS)).message("Please use: hh:mm am/pm."),this.name("email").using(this.aggregate(this.isAllValid,this.isValidEmail)).message("Please enter a valid E-mail address."),this.name("float").using(this.aggregate(this.isAllValid,this.isValidFloat)).message(this.minMaxMessage({minAndMax:"Please enter a number from #{0} to #{1}.",min:"Please enter a number no less than #{0}.",max:"Please enter a number no greater than #{0}.",invalid:"Please enter a number."})),this.name("int").using(this.aggregate(this.isAllValid,this.isValidInt)).message(this.minMaxMessage({minAndMax:"Please enter a number from #{0} to #{1}.",min:"Please enter a number no less than #{0}.",max:"Please enter a number no greater than #{0}.",invalid:"Please enter a number."})),this.name("moneyUS").using(this.aggregate(this.isAllValid,this.isValidMoneyUS)).message(this.minMaxMessage({minAndMax:"Please enter a dollar amount from #{0} to #{1}.",min:"Please enter a dollar amount no less than #{0}.",max:"Please enter a dollar amount no greater than #{0}.",invalid:"Please enter a dollar amount."},function(e){return e.toFixed(2)})),this.name("never").using(this.aggregate(this.isAllValid,this.never)).message("There was an error."),this.name("oneRequired").grouped().using(this.aggregate(this.isAnyValid,this.isPresent)).message("Specify at least one."),this.name("phoneUS").using(this.aggregate(this.isAllValid,this.isValidPhoneUS)).message("Please enter a valid phone number."),this.name("required").using(this.aggregate(this.isAllValid,this.isPresent)).message("This field is required."),this.name("same").grouped().using(this.aggregate(this.passThrough,this.isSame)).message("These values must all match."),this.name("string").using(this.aggregate(this.isAllValid,this.isValidString)).message(this.minMaxMessage({minAndMax:"Please enter a string with length #{0} to #{1}.",min:"Please enter a string with length at least #{0}.",max:"Please enter a string with length no greater than #{0}."}))},e.Guards.prototype.version="1.1.0",e.Guards.prototype.parentContext=function(t){var n=e(t),r=n.parents("form:first");return r.size()===0&&(r=n.parents("*:last")),r},e.Guards.prototype.name=function(t){var n=new e.Guard(null,this,!0);return this.named[t]=n,n},e.Guards.prototype.aggregate=function(t,n){var r=this,i=function(){var i=e.makeArray(arguments);return function(s){return t.call(r,s,function(t){return n.apply(r,e.merge([t],i))})}};return i.acceptsArguments=!0,i},e.Guards.prototype.arrayMessage=function(t){var n=this;return function(r){return n.format(t,e.map(r,function(t){return e.trim(""+t)}).join(", "))}},e.Guards.prototype.minMaxMessage=function(t,n){var r=this;return function(i){r.isNullOrUndefined(i)&&(i={}),e.isFunction(n)||(n=function(e){return e});var s=!r.isNullOrUndefined(i.min),o=!r.isNullOrUndefined(i.max);return s&&o?r.format(t.minAndMax,n(i.min),n(i.max)):s?r.format(t.min,n(i.min)):o?r.format(t.max,n(i.max)):t.invalid?t.invalid:r.defaults.messages["undefined"]}},e.Guards.prototype.log=function(e){console&&console.log&&console.log(e)},e.Guards.prototype.on=function(t,n,r){e.fn.on?e(document).on(n,t,r):e.fn.delegate?e(document).delegate(t,n,r):e.fn.live?e(t).live(n,r):this.log("Could not bind live handlers, probably because jQuery is too old.")},e.Guards.prototype.off=function(t,n,r){e.fn.off?e(document).off(n,t,r):e.fn.undelegate?e(document).undelegate(t,n,r):e.fn.die?e(t).die(n,r):this.log("Could not unbind live handlers, probably because jQuery is too old.")},e.Guards.prototype.enableGuards=function(e){this.on(e,"submit",this.defaults.submitCallback)},e.Guards.prototype.disableGuards=function(e){this.off(e,"submit",this.defaults.submitCallback)},e.Guards.prototype.liveGuard=function(e){this.enableGuards(e),this.on(e,"change blur",this.defaults.liveCallback)},e.Guards.prototype.disableLiveGuard=function(e){this.disableGuards(e),this.off(e,"change blur",this.defaults.liveCallback)},e.Guards.prototype.format=function(){var e=arguments[0];if(arguments.length>1)for(var t=1;t=t.min,r=this.isNullOrUndefined(t.max)||e<=t.max;return n&&r},e.Guards.prototype.isValidInt=function(t,n){return t=e.trim(t),t===""?!0:/^(-|\+)?\d+$/.test(t)?(t=parseInt(t,10),this.isInRange(t,n)):!1},e.Guards.prototype.isValidFloat=function(t,n){return t=e.trim(t),t===""?!0:/^(-|\+)?(\d+)?\.?\d+$/.test(t)?(t=parseFloat(t),this.isInRange(t,n)):!1},e.Guards.prototype.isValidMoneyUS=function(t,n){t=e.trim(t);if(t==="")return!0;if(!/^\$?(-|\+)?\$?([\d,]+)?\.?\d+$/.test(t))return!1;var r=t.indexOf("$");return r>=0&&t.indexOf("$",r+1)>=0?!1:t.indexOf(",")>=0&&!/^\$?(-|\+)?\$?[1-9]\d{0,2}(,\d{3,3})+(\.\d+)?$/.test(t)?!1:t.indexOf(".")>=0&&/\.\d{3,}$/.test(t)?!1:(t=parseFloat(t.replace(/[\$,]/g,"")),this.isInRange(t,n))},e.Guards.prototype.isValidDateUS=function(t,n){return t=e.trim(t),t===""?!0:/^(0?[1-9]|1[0-2])\/(0?[1-9]|[1-2]\d|3[0-1])\/(\d{3}\d+)$/.test(t)},e.Guards.prototype.isValidTimeUS=function(t,n){return t=e.trim(t),t===""?!0:/^(0?[1-9]|1[0-2]):([0-5]\d)\s*(a|p)m$/i.test(t)},e.Guards.prototype.isValidEmail=function(e,t){if(t&&t.allowDisplay){var n=/.*<([^>]+)>\s*$/.exec(e);n&&(e=n[1])}return e===""||/^((([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.test(e)},e.Guards.prototype.isValidPhoneUS=function(e){return e=e.replace(/\s+/g,""),e===""||e.length>9&&e.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/)},e.Guards.prototype.isValidString=function(t,n){return t=e.trim(t),this.isValidInt(""+t.length,n)},e.Guards.prototype.never=function(){return!0},e.Guards.prototype.passThrough=function(t,n){return e.isArray(t)||(t=[t]),n(t)},e.Guards.prototype.add=function(t){var n=new e.Guard(t,this);return this._guards.push(n),n},e.Guards.prototype.guard=function(e){var t=e.guardableFields().clearErrors(),n=this.applyGuards(function(){return t});return t.filter(":visible:has-error").eq(0).focus(),n},e.Guards.prototype.applyGuards=function(t){var n=!0,r=this;return e.each(this._guards,function(e,i){var s=t(i);s!==!1&&!r.test(i,s)&&(n=!1)}),n},e.Guards.prototype.test=function(e,t){if(e.isGrouped())return e.test(t);var n=!0;return t.each(function(){e.test(this)||(n=!1)}),n},e.Guard=function(t,n,r){this._named=r,this._guards=n||e.guards,this._selector=t,this._guard=null,r||this.using(this._guards.defaults.guard)},e.Guard.prototype.cloneGuard=function(t,n){var r=this,i=this._guards.named[t];if(this._guards.isNullOrUndefined(i))throw new Error("There is no named guard '"+t+"'");var s=function(e){if(r[e]!==undefined||i[e]===undefined)return;r[e]=i[e]};return s("_grouped"),s("_tag"),s("_messageClass"),s("_invalidClass"),s("_target"),s("_precondition"),this._guard=i._guard,this.name=t,this._guard.acceptsArguments&&(this._guard=this._guard.apply(this._guards,n)),e.isFunction(i._message)?this.message(i._message.apply(this._guards,n)):this.message(i._message)},e.Guard.prototype.using=function(t){if(typeof t=="string"){var n=[];return arguments.length>1&&(n=e.makeArray(arguments).slice(1)),this.cloneGuard(t,n)}return this._guard=t,this.message(this._guards.defaults.messages["undefined"])},e.Guard.prototype.getPrecondition=function(){return this._precondition===undefined?this._guards.defaults.precondition:this._precondition},e.Guard.prototype.precondition=function(e){return this._precondition=e,this},e.Guard.prototype.isGrouped=function(){return this._grouped===undefined?this._guards.defaults.grouped:this._grouped},e.Guard.prototype.grouped=function(){return arguments.length===0?this.grouped(!0):(this._grouped=arguments[0],this)},e.Guard.prototype.getTag=function(){return this._tag===undefined?this._guards.defaults.tag:this._tag},e.Guard.prototype.tag=function(e){return this._tag=e,this.resetMessageFn()},e.Guard.prototype.getMessageClass=function(){return this._messageClass===undefined?this._guards.defaults.messageClass:this._messageClass},e.Guard.prototype.messageClass=function(e){return this._messageClass=e,this.resetMessageFn()},e.Guard.prototype.message=function(e){return this._message=e,this.resetMessageFn()},e.Guard.prototype.getInvalidClass=function(){return this._invalidClass===undefined?this._guards.defaults.invalidClass:this._invalidClass},e.Guard.prototype.invalidClass=function(e){return this._invalidClass=e,this},e.Guard.prototype.resetMessageFn=function(){var t=this;return this.messageFn(function(){return e("<"+t.getTag()+' class="'+t.getMessageClass()+'"/>').html(t._message)})},e.Guard.prototype.messageFn=function(e){return this._messageFn=e,this},e.Guard.prototype.errorElement=function(){var e=this._messageFn();return e[0].isGuardError=!0,e},e.Guard.prototype.attachError=function(t,n){var r=this.getTarget();if(r&&e.isFunction(r)){var i=r.call(t,n);i!==!1&&n.appendTo(e(i).eq(0))}else{if(!r)throw new Error("The target must be a function or selector!");n.appendTo(e(r).eq(0))}},e.Guard.prototype.getTarget=function(){return this._target===undefined?this._guards.defaults.target:this._target},e.Guard.prototype.target=function(e){return this._target=e,this},e.Guard.prototype.appliesTo=function(t){return e(t).filter(this._selector).size()>0},e.Guard.prototype.test=function(t){var n=e(t).filter(this._selector);if(n.size()===0)return!0;if(!this._guards.options.stackErrors&&n.hasErrors())return!1;var r,i,s;this.isGrouped()?(s=[],i=[],n.each(function(){s.push(e(this).inputValue(this._guards)),i.push(this)})):(s=n.inputValue(this._guards),i=t);if(!this.testPrecondition(s,i))r=!0;else try{r=this._guard(s,i)}catch(o){this._guards.log("A guard threw an error: "+o),r=!1}return r||this.triggerError(n),r},e.Guard.prototype.testPrecondition=function(e,t){var n=this.getPrecondition();if(!n)return!0;try{return n(e,t)!==!1}catch(r){return this._guards.log("A precondition threw an error: "+r),!1}},e.Guard.prototype.triggerError=function(){var t;if(arguments.length===0)t=this._selector;else{if(arguments.length!==1)throw new Error("Expected 0 or 1 argument to triggerError, got "+arguments.length);t=arguments[0]}return this.isGrouped()?e(t).addSingleError(this):e(t).addError(this),this},e.Guard.prototype.sendEvent=function(t,n,r,i){var s=e.Event(t);s.guard=this,s.errorElements=n.toArray();var o=n;return r&&(o=o.parents("form")),i&&(s.errorMessage=e(i)[0]),o.trigger(s),s},e.GuardError=function(e,t,n,r){this._guard=e,this._element=t,this._errorElement=n,this._linked=r,this._cleared=!1},e.GuardError.prototype.clear=function(t){if(this._cleared)return;var n=[],r;if(!t){for(var i=0;i=0&&this._element.errors.splice(u,1),a.hasErrorsWithInvalidClass(this._guard.getInvalidClass())||a.removeClass(this._guard.getInvalidClass()),this._cleared=!0;while(this._linked.length>0)this._linked.shift().clear(!0);t||(this._guard.sendEvent("afterClearGuardError",r,!1),this._guard.sendEvent("afterClearGuardFormError",r,!0))},e.fn.guardableFields=function(){return this.find(":guardable")},e.fn.guard=function(){return e.guards.guard(this)},e.fn.triggerError=function(e){e.triggerError(this)},e.fn.addSingleError=function(t){if(this.size()===0)return e.guards.log("Attempted to add error to nothing."),this;if(this.hasError(t))return this;var n=t.sendEvent("guardError",this).isDefaultPrevented(),r=t.sendEvent("guardFormError",this,!0).isDefaultPrevented();if(n||r)return this;var i=t.errorElement();t.attachError(this,i),this.addClass(t.getInvalidClass());var s=[];return this.each(function(){this.errors||(this.errors=[]);var n=new e.GuardError(t,this,i,s);s.push(n),this.errors.push(n)}),t.sendEvent("afterGuardError",this,!1,i),t.sendEvent("afterGuardFormError",this,!0,i),this},e.fn.addError=function(t){var n={};return this.each(function(){var r=e(this);if(r.is(":radio")){var i=r.attr("name");if(n[i])return;n[i]=!0;var s=t._guards.parentContext(r),o=e("input[name='"+i+"']:radio",s);o.addSingleError(t)}else r.addSingleError(t)})},e.fn.errors=function(){var e=[];return this.each(function(){this.errors&&this.errors.length>0&&e.push.apply(e,this.errors)}),e},e.fn.clearErrors=function(){return e.each(this.errors(),function(e,t){t.clear()}),this},e.fn.hasError=function(t){var n=!1;return e.each(this.errors(),function(e,r){if(r._guard===t)return n=!0,!1}),n},e.fn.hasErrors=function(){return this.errors().length>0},e.fn.hasErrorsWithInvalidClass=function(t){var n=!1;return e.each(this.errors(),function(e,r){if(r._guard.getInvalidClass()===t)return n=!0,!1}),n},e.fn.inputValue=function(t){t=t||e.guards;if(this.is(":radio")){var n=e("input[name='"+this.attr("name")+"']:radio:checked",t.parentContext(this));return n.size()===0?t.constants.notChecked:n.val()}return this.is(":checkbox")?this.is(":checked")?this.val():t.constants.notChecked:this.val()},e.fn.enableGuards=function(){return this.bind("submit",e.guards.defaults.submitCallback)},e.fn.disableGuards=function(){return this.unbind("submit",e.guards.defaults.submitCallback)},e.enableGuards=function(t){e.guards.enableGuards(t)},e.disableGuards=function(t){e.guards.disableGuards(t)},e.liveGuard=function(t){e.guards.liveGuard(t)},e.disableLiveGuard=function(t){e.guards.disableLiveGuard(t)},e.extend(e.expr[":"],{"has-error":function(e){return!!(e.errors&&e.errors.length>0)},guardable:function(e){var t=e.tagName.toLowerCase();return t==="input"||t==="textarea"||t==="select"||t==="button"}}),e.guards=new e.Guards;var t=function(){e(this).clearErrors()};e.guards.on(":has-error","change",t),e.guards.on(":has-error:radio,:has-error:checkbox","mouseup",t),e.guards.on("select:has-error","mousedown",t),e.guards.on(":has-error","keydown",function(){this.clearable=!0}),e.guards.on(":has-error","keyup",function(){this.clearable&&(this.clearable=!1,e(this).clearErrors())})})(jQuery);