/*! * Guards JavaScript jQuery Plugin v0.7.0 * http://github.com/on-site/Guards-Javascript-Validation * * 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 Feb 22 22:35:49 2013 -0800 */ (function(e){e.guard=function(t){return e.guards.add(t)},e.guard.version="0.7.0",e.Guards=function(){this._guards=[],this.options={stackErrors:!1},this.constants={notChecked:""};var t=function(t,n){return function(){var r=e.makeArray(arguments);return function(i,s){return e.guards[t](i,function(t){return e.guards[n].apply(e.guards[n],e.merge([t],r))})}}},n=function(t,n){return function(r){e.guards.isNullOrUndefined(r)&&(r={}),e.isFunction(n)||(n=function(e){return e});var i=!e.guards.isNullOrUndefined(r.min),s=!e.guards.isNullOrUndefined(r.max);return i&&s?e.guards.format(t.minAndMax,n(r.min),n(r.max)):i?e.guards.format(t.min,n(r.min)):s?e.guards.format(t.max,n(r.max)):t.invalid?t.invalid:e.guards.defaults.messages.undefined}},r=function(t){return function(n){return e.guards.format(t,e.map(n,function(t,n){return e.trim(""+t)}).join(", "))}};this.defaults={grouped:!1,guard:"required",guards:{allow:t("isAllValid","isAllowed"),always:t("isAllValid","always"),different:t("passThrough","isDifferent"),disallow:t("isAllValid","isDisallowed"),email:t("isAllValid","isValidEmail"),"float":t("isAllValid","isValidFloat"),"int":t("isAllValid","isValidInt"),moneyUS:t("isAllValid","isValidMoneyUS"),never:t("isAllValid","never"),oneRequired:t("isAnyValid","isPresent"),phoneUS:t("isAllValid","isValidPhoneUS"),required:t("isAllValid","isPresent"),same:t("passThrough","isSame"),string:t("isAllValid","isValidString")},invalidClass:"invalid-field",messageClass:"error-message",messages:{allow:r("Please enter one of: #{0}."),always:"There was an error.",different:"These values must all be different.",disallow:r("Please don't enter: #{0}."),email:"Please enter a valid E-mail address.","float":n({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."}),"int":n({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."}),moneyUS:n({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)}),never:"There was an error.",oneRequired:"Specify at least one.",phoneUS:"Please enter a valid phone number.",required:"This field is required.",same:"These values must all match.",string:n({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}."}),"undefined":"Please fix this field."},tag:"span",target:function(t){var n=e(this).filter(":last");return n.is(":radio,:checkbox")&&(n=e(n[0].nextSibling)),t.insertAfter(n),!1}}},e.Guards.prototype.version="0.7.0",e.Guards.prototype.isArray=e.isArray;if(!e.Guards.prototype.isArray){var t=[].constructor,n=jQuery;e.Guards.prototype.isArray=function(e){return e.constructor==t||e.constructor==n}}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.format=function(){var e=arguments[0];if(arguments.length>1)for(var t=1;t=n.min,i=e.guards.isNullOrUndefined(n.max)||t<=n.max;return r&&i},e.Guards.prototype.isValidInt=function(t,n){return t=e.trim(t),t==""?!0:/^(-|\+)?\d+$/.test(t)?(t=parseInt(t,10),e.guards.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),e.guards.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,"")),e.guards.isInRange(t,n))},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),e.guards.isValidInt(""+t.length,n)},e.Guards.prototype.never=function(e){return!0},e.Guards.prototype.passThrough=function(e,t){return this.isArray(e)||(e=[e]),t(e)},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(e){return t});return t.filter(":visible:has-error").eq(0).focus(),n},e.Guards.prototype.applyGuards=function(t){var n=!0;return e.each(this._guards,function(r,i){var s=t(i);s!==!1&&!e.guards.test(i,s)&&(n=!1)}),n},e.Guards.prototype.test=function(e,t){if(e._grouped)return e.test(t);var n=!0;return t.each(function(){e.test(this)||(n=!1)}),n},e.Guard=function(t,n){this._guards=n||e.guards,this._selector=t,this._grouped=this._guards.defaults.grouped,this._tag=this._guards.defaults.tag,this._messageClass=this._guards.defaults.messageClass,this._invalidClass=this._guards.defaults.invalidClass,this._target=this._guards.defaults.target,this.using(this._guards.defaults.guard)},e.Guard.prototype.using=function(t){if(typeof t=="string"){var n=[];arguments.length>1&&(n=e.makeArray(arguments).slice(1));var r=this._guards.defaults.guards[t];if(e.guards.isNullOrUndefined(r))throw new Error("There is no standard guard named '"+t+"'");this._guard=r.apply(this._guards.defaults.guards,n);var i=this._guards.defaults.messages[t];return e.isFunction(i)&&(i=i.apply(this._guards.defaults.messages,n)),this.message(i)}return this._guard=t,this.message(this._guards.defaults.messages.undefined)},e.Guard.prototype.precondition=function(e){return this._precondition=e,this},e.Guard.prototype.isGrouped=function(){return this._grouped},e.Guard.prototype.grouped=function(){return arguments.length==0?this.grouped(!0):(this._grouped=arguments[0],this)},e.Guard.prototype.tag=function(e){return this._tag=e,this.resetMessageFn()},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.invalidClass=function(e){return this._invalidClass=e,this},e.Guard.prototype.resetMessageFn=function(){var t=this;return this.messageFn(function(){return e("<"+t._tag+' class="'+t._messageClass+'"/>').html(t._message)})},e.Guard.prototype.messageFn=function(e){return this._messageFn=e,this},e.Guard.prototype.errorElement=function(){return this._messageFn()},e.Guard.prototype.attachError=function(t,n){if(this._target&&e.isFunction(this._target)){var r=this._target.call(t,n);r!==!1&&n.appendTo(e(r).eq(0))}else{if(!this._target)throw new Error("The target must be a function or selector!");n.appendTo(e(this._target).eq(0))}},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(!e.guards.options.stackErrors&&n.hasErrors())return!1;var r;if(this._grouped){var i=[],s=[];n.each(function(){i.push(e(this).inputValue(this._guards)),s.push(this)}),this._precondition&&this._precondition(i,s)===!1?r=!0:r=this._guard(i,s)}else{var o=n.inputValue(this._guards);this._precondition&&this._precondition(o,t)===!1?r=!0:r=this._guard(o,t)}return r||this.triggerError(n),r},e.Guard.prototype.triggerError=function(t){return this._grouped?e(t).addSingleError(this):e(t).addError(this),this},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(){if(this._cleared)return;this._errorElement.remove();var t=e.inArray(this,this._element.errors);t>=0&&this._element.errors.splice(t,1),e(this._element).hasErrorsWithInvalidClass(this._guard._invalidClass)||e(this._element).removeClass(this._guard._invalidClass),this._cleared=!0;while(this._linked.length>0)this._linked.shift().clear()},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;var n=t.errorElement();t.attachError(this,n),this.addClass(t._invalidClass);var r=[];return this.each(function(){this.errors||(this.errors=[]);var i=new e.GuardError(t,this,n,r);r.push(i),this.errors.push(i)})},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=e("input[name='"+i+"']:radio",r.parents("form"));s.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.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._invalidClass==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",this.parents("form"));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.submit(function(){return e(this).guard()})},e.enableGuards=function(t){e.guards.on(t,"submit",function(){return e(this).guard()})},e.liveGuard=function(t){e.enableGuards(t),e.guards.on(t,"change",function(t){var n=e(t.srcElement);if(!n.is(":guardable"))return;e.guards.applyGuards(function(e){return e.isGrouped()?e.appliesTo(n)?n.parents("form:first").find(":guardable"):!1:n})})},e.extend(e.expr[":"],{"has-error":function(e){return(new Boolean(e.errors&&e.errors.length>0)).valueOf()},guardable:function(e){return e.tagName.toLowerCase()=="input"||e.tagName.toLowerCase()=="textarea"||e.tagName.toLowerCase()=="select"}}),e.guards=new e.Guards,e(function(){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);