validator.tool
Version:
Lightweight JavaScript form validation, that had minimal configuration and felt natural to use. No dependencies, support UMD.
4 lines (3 loc) • 2.55 kB
JavaScript
/*! validator.tool v2.2.6 | MIT © 2023 kenny wang <wowohoo@qq.com> (https://github.com/jaywcjlove) http://jaywcjlove.github.io/validator.js */
!function(e,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define(s):(e="undefined"!=typeof globalThis?globalThis:e||self).Validator=s()}(this,(function(){"use strict";var e=function(){return e=Object.assign||function(e){for(var s,t=1,r=arguments.length;t<r;t++)for(var i in s=arguments[t])Object.prototype.hasOwnProperty.call(s,i)&&(e[i]=s[i]);return e},e.apply(this,arguments)};return"function"==typeof SuppressedError&&SuppressedError,function(){function s(s){void 0===s&&(s={});var t=this;this.fields={},this.rules={},this.values={},this.messagesShown=!1,this.errorMessages={},this.showMessages=function(){return t.messagesShown=!0},this.hideMessages=function(){return t.messagesShown=!1},this.getForm=function(){return t.form},this.setForm=function(e){t.form=e,t.values={};var s=new FormData(t.form);s.forEach((function(e,r){var i=s.getAll(r),a=t.form[r];a&&("checkbox"===a.type?t.values[r]=a.checked:a.multiple?t.values[r]=i:a.type?t.values[r]=s.get(r):i.length>0&&!a.value?t.values[r]=i:t.values[r]=a.value),t.message(r,t.values[r],t.rules[r])})),t.initValues||(t.initValues=t.values)},this.message=function(e,s,r){t.fields[e]=!0,t.values[e]=s,r&&(t.rules[e]=r);var i=(t.rules[e]||{}).validate,a="";if(t.validate&&(a=t.validate(t.values[e],t.values,e)),i&&(a=i(t.values[e],t.values,e)),a?(t.fields[e]=!1,t.errorMessages[e]=a):delete t.errorMessages[e],t.messagesShown)return t.errorMessages[e]},this.setValues=function(e){if(void 0===e&&(e={}),!t.form)for(var s in e)t.message(s,e[s])},this.getValues=function(){return t.form&&t.setForm(t.form),t.values},this.reset=function(){return t.values=e({},t.initValues),t.errorMessages={},t.messagesShown=!1,t.fields={},t.form&&t.form.reset(),e({},t.values)},this.fieldValid=function(e){return!0===t.fields[e]};var r=s.form,i=s.rules,a=s.initValues,n=s.messagesShown,o=s.validate;this.messagesShown=!!n,this.rules=i||{},this.validate=o,a&&(this.values=e({},a),this.initValues=e({},a)),r&&this.setForm(r)}return Object.defineProperty(s.prototype,"resetInitValue",{set:function(e){this.initValues=e,this.form&&this.setForm(this.form)},enumerable:!1,configurable:!0}),s.prototype.allValid=function(){for(var e in this.values)this.message(e,this.values[e],this.rules[e]);for(var s in this.fields)if(!1===this.fieldValid(s))return!1;return!0},s}()}));
//# sourceMappingURL=validator.min.js.map