1
0
mirror of https://github.com/e107inc/e107.git synced 2025-01-17 12:48:24 +01:00
php-e107/e107_web/js/jquery.h5validate.min.js

1 line
11 KiB
JavaScript

(function($){"use strict";var console=window.console||function(){},h5={defaults:{debug:!1,RODom:!1,patternLibrary:{phone:/([\+][0-9]{1,3}([ \.\-])?)?([\(]{1}[0-9]{3}[\)])?([0-9A-Z \.\-]{1,32})((x|ext|extension)?[0-9]{1,4}?)/,email:/((([a-zA-Z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-zA-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-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-zA-Z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-zA-Z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uF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uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|[\uE000-\uF8FF]|\/|\?)*)?(\#((([a-zA-Z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(%[\da-f]{2})|[!\$&'\(\)\*\+,;=]|:|@)|\/|\?)*)?/,number:/-?(?:\d+|\d{1,3}(?:,\d{3})+)?(?:\.\d+)?/,dateISO:/\d{4}[\/\-]\d{1,2}[\/\-]\d{1,2}/,alpha:/[a-zA-Z]+/,alphaNumeric:/\w+/,integer:/-?\d+/},classPrefix:"h5-",errorClass:"ui-state-error",validClass:"ui-state-valid",activeClass:"active",requiredClass:"required",requiredAttribute:"required",patternAttribute:"pattern",errorAttribute:"data-h5-errorid",customEvents:{validate:!0},kbSelectors:":input:not(:button):not(:disabled):not(.novalidate)",focusout:!0,focusin:!1,change:!0,keyup:!1,activeKeyup:!0,mSelectors:'[type="range"]:not(:disabled):not(.novalidate), :radio:not(:disabled):not(.novalidate), :checkbox:not(:disabled):not(.novalidate), select:not(:disabled):not(.novalidate), option:not(:disabled):not(.novalidate)',click:!0,requiredVar:"h5-required",patternVar:"h5-pattern",stripMarkup:!0,submit:!0,focusFirstInvalidElementOnSubmit:!0,validateOnSubmit:!0,invalidCallback:function(){},validCallback:function(){},allValidSelectors:":input:visible:not(:button):not(:disabled):not(.novalidate)",markInvalid:function(options){var $element=$(options.element),$errorID=$(options.errorID);return $element.addClass(options.errorClass).removeClass(options.validClass),$element.addClass(options.settings.activeClass),$errorID.length&&($element.attr("title")&&$errorID.text($element.attr("title")),$errorID.show()),$element.data("valid",!1),options.settings.invalidCallback.call(options.element,options.validity),$element},markValid:function(options){var $element=$(options.element),$errorID=$(options.errorID);return $element.addClass(options.validClass).removeClass(options.errorClass),$errorID.length&&$errorID.hide(),$element.data("valid",!0),options.settings.validCallback.call(options.element,options.validity),$element},unmark:function(options){var $element=$(options.element);return $element.removeClass(options.errorClass).removeClass(options.validClass),$element.form.find("#"+options.element.id).removeClass(options.errorClass).removeClass(options.validClass),$element}}},defaults=h5.defaults,patternLibrary=defaults.patternLibrary,createValidity=function createValidity(validity){return $.extend({customError:validity.customError||!1,patternMismatch:validity.patternMismatch||!1,rangeOverflow:validity.rangeOverflow||!1,rangeUnderflow:validity.rangeUnderflow||!1,stepMismatch:validity.stepMismatch||!1,tooLong:validity.tooLong||!1,typeMismatch:validity.typeMismatch||!1,valid:validity.valid||!0,valueMissing:validity.valueMissing||!1},validity)},methods={isValid:function(settings,options){var $this=$(this);return options=settings&&options||{},options.revalidate!==!1&&$this.trigger("validate"),$this.data("valid")},allValid:function(config,options){var $allFields,$filteredFields,valid=!0,formValidity=[],$this=$(this),radioNames=[],getValidity=function getValidity(e,data){data.e=e,formValidity.push(data)},settings=$.extend({},config,options);return options=options||{},$this.trigger("formValidate",{settings:$.extend(!0,{},settings)}),$this.undelegate(settings.allValidSelectors,".allValid",getValidity),$this.delegate(settings.allValidSelectors,"validated.allValid",getValidity),$allFields=$this.find(settings.allValidSelectors),$filteredFields=$allFields.filter(function(){var name;if("INPUT"===this.tagName&&"radio"===this.type){if(name=this.name,radioNames[name]===!0)return!1;radioNames[name]=!0}return!0}),$filteredFields.each(function(){var $this=$(this);valid=$this.h5Validate("isValid",options)&&valid}),$this.trigger("formValidated",{valid:valid,elements:formValidity}),valid},validate:function(settings){var maxlength,$this=$(this),pattern=$this.filter("[pattern]")[0]?$this.attr("pattern"):!1,re=RegExp("^(?:"+pattern+")$"),$radiosWithSameName=null,value=$this.is("[type=checkbox]")?$this.is(":checked"):$this.is("[type=radio]")?($radiosWithSameName=$this.parents("form").find('input[name="'+$this.attr("name")+'"]')).filter(":checked").length>0:$this.val(),errorClass=settings.errorClass,validClass=settings.validClass,errorIDbare=$this.attr(settings.errorAttribute)||!1,errorID=errorIDbare?"#"+errorIDbare.replace(/(:|\.|\[|\])/g,"\\$1"):!1,required=!1,validity=createValidity({element:this,valid:!0}),$checkRequired=$("<input required>");required=$checkRequired.filter("[required]")&&$checkRequired.filter("[required]").length?$this.filter("[required]").length&&"false"!==$this.attr("required"):void 0!==$this.attr("required"),settings.debug&&window.console&&(console.log('Validate called on "'+value+'" with regex "'+re+'". Required: '+required),console.log("Regex test: "+re.test(value)+", Pattern: "+pattern)),maxlength=parseInt($this.attr("maxlength"),10),!isNaN(maxlength)&&value.length>maxlength&&(validity.valid=!1,validity.tooLong=!0),required&&!value?(validity.valid=!1,validity.valueMissing=!0):pattern&&!re.test(value)&&value?(validity.valid=!1,validity.patternMismatch=!0):settings.RODom||settings.markValid({element:this,validity:validity,errorClass:errorClass,validClass:validClass,errorID:errorID,settings:settings}),validity.valid||settings.RODom||settings.markInvalid({element:this,validity:validity,errorClass:errorClass,validClass:validClass,errorID:errorID,settings:settings}),$this.trigger("validated",validity),null!==$radiosWithSameName&&settings.alreadyCheckingRelatedRadioButtons!==!0&&(settings.alreadyCheckingRelatedRadioButtons=!0,$radiosWithSameName.not($this).trigger("validate"),settings.alreadyCheckingRelatedRadioButtons=!1)},delegateEvents:function(selectors,eventFlags,element,settings){var events={},key=0,validate=function(){settings.validate.call(this,settings)};$.each(eventFlags,function(key,value){value&&(events[key]=key)});for(key in events)events.hasOwnProperty(key)&&$(element).delegate(selectors,events[key]+".h5Validate",validate);return element},bindDelegation:function(settings){var $forms,$this=$(this);return $.each(patternLibrary,function(key,value){var pattern=""+value;pattern=pattern.substring(1,pattern.length-1),$("."+settings.classPrefix+key).attr("pattern",pattern)}),$forms=$this.filter("form").add($this.find("form")).add($this.parents("form")),$forms.attr("novalidate","novalidate").submit(checkValidityOnSubmitHandler),$forms.find("input[formnovalidate][type='submit']").click(function(){$(this).closest("form").unbind("submit",checkValidityOnSubmitHandler)}),this.each(function(){var kbEvents={focusout:settings.focusout,focusin:settings.focusin,change:settings.change,keyup:settings.keyup},mEvents={click:settings.click},activeEvents={keyup:settings.activeKeyup};settings.delegateEvents(":input",settings.customEvents,this,settings),settings.delegateEvents(settings.kbSelectors,kbEvents,this,settings),settings.delegateEvents(settings.mSelectors,mEvents,this,settings),settings.delegateEvents(settings.activeClassSelector,activeEvents,this,settings),settings.delegateEvents("textarea[maxlength]",{keyup:!0},this,settings)})}},checkValidityOnSubmitHandler=function(evt){var $this,allValid,settings=getInstance.call(this);if(settings.submit===!0){if($this=$(this),allValid=$this.h5Validate("allValid",{revalidate:settings.validateOnSubmit===!0}),allValid!==!0&&(evt.preventDefault(),settings.focusFirstInvalidElementOnSubmit===!0)){var $invalid=$(settings.allValidSelectors,$this).filter(function(){return $(this).h5Validate("isValid",{revalidate:!1})!==!0});$invalid.first().focus()}return allValid}},instances=[],buildSettings=function buildSettings(options){var settings=$.extend({},defaults,options,methods),activeClass=settings.classPrefix+settings.activeClass;return $.extend(settings,{activeClass:activeClass,activeClassSelector:"."+activeClass,requiredClass:settings.classPrefix+settings.requiredClass,el:this})},getInstance=function getInstance(){var $parent=$(this).closest("[data-h5-instanceId]");return instances[$parent.attr("data-h5-instanceId")]},setInstance=function setInstance(settings){var instanceId=instances.push(settings)-1;settings.RODom!==!0&&$(this).attr("data-h5-instanceId",instanceId),$(this).trigger("instance",{"data-h5-instanceId":instanceId})};$.h5Validate={addPatterns:function(patterns){var key,patternLibrary=defaults.patternLibrary;for(key in patterns)patterns.hasOwnProperty(key)&&(patternLibrary[key]=patterns[key]);return patternLibrary},validValues:function(selector,values){var re,i=0,ln=values.length,pattern="";for(i=0;ln>i;i+=1)pattern=pattern?pattern+"|"+values[i]:values[i];re=RegExp("^(?:"+pattern+")$"),$(selector).data("regex",re)}},$.fn.h5Validate=function(options){var action,args,settings;return"string"==typeof options&&"function"==typeof methods[options]?(settings=getInstance.call(this),args=[].slice.call(arguments,0),action=options,args.shift(),args=$.merge([settings],args),settings[action].apply(this,args)):(settings=buildSettings.call(this,options),setInstance.call(this,settings),methods.bindDelegation.call(this,settings))}})(jQuery);