├── README.md └── src └── meanie.js /README.md: -------------------------------------------------------------------------------- 1 | Blue Meanie 2 | ----------- 3 | No form is safe in Pepperland. 4 | 5 | What is Blue Meanie? 6 | -------------------- 7 | Blue Meanie is a simple form validation engine. It accepts rules, validates form elements (event based), and triggers events. 8 | It does not care how you render error messages or maintain state. It is a jQuery plugin, of course. 9 | 10 | Disclaimer 11 | ---------- 12 | As of publishing, 01/10/2013, Blue Meanie has some rough edges and needs some improvement. Blue Meanie is currently being used in a limited case. 13 | Blue Meanie will be refined as/if the scope of usage increases. Blue Meanie is being used as part of larger validation library that contains state 14 | management and rendering modules. The modules are then tied together through a common API. Documentation and examples will be added as time permits. 15 | 16 | How Does it Work? 17 | ----------------- 18 | It listens for certain DOM events and validates when the events occur. 19 | 20 | First thing you will need are some rules. Rules can be loaded like so: 21 | ```javascript 22 | $.fn.meanie('addRule', { // this will add the rule to the plugin itself not an instance of the plugin, so that rules can be shared with all instances 23 | name: 'required', 24 | msg: 'This field is required.', 25 | test: function (target, options) { 26 | // target is the input being validated 27 | // an options argument can be defined when a rule set is created (see example later on) 28 | return true; // this should return a boolean value 29 | } 30 | }); 31 | ``` 32 | 33 | Now that we have a rule lets validate a form: 34 | ```javascript 35 | $('form').meanie({ 36 | rulesets: [ // there can be an infinite number of rule sets that contain an infinite number of rules 37 | { 38 | qrysel: 'input[type="text"]', // THIS IS COOL: apply this rule to all elements that match this query selector 39 | rules: [ 40 | { 41 | // use the required rule we defined above; options is an optional argument; 42 | // options will be passed as an argument to validation function (see addRule example above) 43 | { name: 'required', options: {} }, 44 | { name: 'number' } // pretend this rule has already been defined 45 | } 46 | ] 47 | } 48 | ] 49 | }); 50 | ``` 51 | 52 | Lets listen for validation events: 53 | ```javascript 54 | $('form').on('meanie.validate', function (event, verdicts) { 55 | // when an input is validated an event is triggered 56 | // an array of verditc objects is returned; each object cotains the following properties 57 | // $target, msg, options, rules, valid 58 | }); 59 | ``` 60 | 61 | Validate this element: 62 | ```javascript 63 | // returns an array of verdict objects 64 | $('form').meanie('validate', target, silent); // target is a DOM element; silent is a bool indicating whether an event should be triggered 65 | ``` 66 | 67 | Let me see all the rules: 68 | ```javascript 69 | $('form').meanie('rules'); 70 | ``` 71 | 72 | No longer validate this element: 73 | ```javascript 74 | $('form').meanie('remove', target); // target is a DOM element 75 | ``` 76 | 77 | We are done validating. Lets remove the validation: 78 | ```javascript 79 | $('form').meanie('destroy'); 80 | ``` 81 | 82 | License 83 | ------- 84 | Copyright © 2012-2013 by Teradata Corporation. 85 | 86 | Issued under the MIT License 87 | 88 | Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 89 | 90 | The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 91 | 92 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF 93 | 94 |
 95 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 96 | ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
 97 | ::::::::::::::::::::::::::::::::::::::''``      ``''::::::::::::::::::::::
 98 | ::::::::::::::::::::::::::::::::::''                 `':::::::::::::::::::
 99 | ::::::::::::::::::::::::::::::::'                      `':::::::::::::::::
100 | ::::::::::::::::::::::::::::::'                          :::::::::::::::::
101 | :::::::::::::::::::::::::::::'                            ::::::::::::::::
102 | :::::::::::::::::::::::::::::                             `:::::::::::::::
103 | :::::::::::::::::::::::::::::                              :::::::::::::::
104 | ::::::::::::::::::::::::::::                               `````''::::::::
105 | ::::::::::::::::::::::::::::                                        `'::::
106 | ::::::::::::::::::::::::::::                                           `::
107 | ::::::::::::::::::::::::::::                                            `:
108 | ::::::::::::::::::::::::::::.
109 | :::::::::::::::::::::::::::::.
110 | ::::::::::::::::::::::::::::::::
111 | ::::::::::::::::::::::::::::''``?.
112 | '`.,.`':::::::::::::::::''       "h
113 | ,d$$$h.`'::::::::::::::'   ,-.    `h
114 | $$$$$$$h. `:::::::::::'   (   )    F                                     :
115 | $$$$$$$$$$cc,_````_,,cc    `-'     ""==,,_                              ::
116 | `?$$$$$$$$$$$$$$$$$$$$$.        ,ccc,.    ""==-,_                     .:::
117 | : "?$$$$$$$$$$$$$$$$$$$$$cccccd$$$$P"  $hcc,.   `:::.              .::::::
118 | :::. "?$$$$$$$$$$$$$$$$$$$$$$$$$$" /`",$$$$$$P .::::::::........::::::::::
119 | ::::::..""?$$$$$$$$$$$$$$$$$$$P".-",= $$$$$$" ::::::::::::::::::::::::::::
120 | :::::::::.. ""??$$$$$$$$$???"".',d". d$$$$$F :::::::::::::::::::::::::::::
121 | :::::::::::::::.;;, .,,,ccc==",dP', z$$$$$" ::::::::::::::::::::::::::::::
122 | :::::::::::::::::::.`""""_,nmPP  ,",$$$$$' :::::::::::::::::::::::::::::::
123 | :::::::::::::::::::: "PPPP""   ,d",$$$$$$ ::::::::::::::::::::::::::::::::
124 | ::::::::::::::::::::::::     ,d" ,$$$$$$F ::::::::::::::::::::::::::::::::
125 | ::::::::::::::::::::'`` ::',dP   $$$$$$$F :'':::::::::::::::::::::::::::::
126 | :::::::::::::::::'`.mbu;;_dP", ,d$$$$$$P      `''````````'::::::::::::::::
127 | ::::::::::::::'' -c,.""""_,=",c$$$$$$$??                  `````'':::::::::
128 | ::::::::::::' ,chc,.""???".,d$$$$$???                             `'''`'::
129 | ::::::::::: ,d$$$$$$$$ccd$$$$$$P""                                      `:
130 | ::::::::::: $$$$$$$$$$$$$$$$???
131 | ::::::::::. $$$$$$$$$$$$$P"
132 | ::::::::::: `?$$$$$$$$$$"
133 | ::::::::::::.,`"??""""??
134 | :::::::::::::
135 | :::::::'``':'
136 | ::::::'
137 | ::'`
138 | 
139 | -------------------------------------------------------------------------------- /src/meanie.js: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright © 2012-2013 by Teradata Corporation. 3 | * 4 | * Issued under the MIT License 5 | * 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), 7 | * to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, 8 | * and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: 9 | * 10 | * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 11 | * 12 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 13 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 14 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 15 | */ 16 | (function ($) { 17 | if (!$) throw 'BLUE MEANIE: What are you thinking? Everything requires jQuery and jQuery is everything.' 18 | 19 | 'use strict'; 20 | 21 | var opts, render, validate, rules = {}, fields = [], trigger, watch, init, 22 | setKeys, saveFormRules, cache = {}, createKey, timeout, getTargetRules, 23 | api = {}, msg = {}, vaildateObjFromTarget, submitEvent = 'submit.chief-meanie', 24 | inlineEvents = 'keyup.glove change.glove paste.glove', validateForm, addInputKey; 25 | 26 | // creates unique keys for hash table lookups 27 | createKey = function () { 28 | var s4 = function () { 29 | return (((1+Math.random())*0x10000)|0).toString(16).substring(1); 30 | }; 31 | 32 | return (s4()+s4()+'-'+s4()+'-'+s4()+'-'+s4()+'-'+s4()+s4()+s4()); 33 | }; 34 | 35 | // public api 36 | api.init = function (o) { 37 | 38 | if ((o.render && o.render === 'bootstrap') || typeof o.render === 'undefined') // default to bootstrap 39 | o.render = render; 40 | else if (typeof o.render === 'function') // developer defined render 41 | render = o.render; 42 | else // no rendering 43 | render = false; 44 | 45 | var $form, opts = $.extend({ 46 | inline: true 47 | }, o); 48 | 49 | return this.each(function () { 50 | $form = $(this); 51 | setKeys(saveFormRules($form, opts)); 52 | watch($form, opts); 53 | }); 54 | }; 55 | 56 | api.rules = function () { // return a copy of the rulesets 57 | var formkey = $.data(this, 'pepperland'), opts = cache[formkey], rulesets = opt ? opt.rulesets : []; 58 | return rulesets.slice(0); 59 | }; 60 | 61 | api.add = function (ruleset, index) { // add a ruleset 62 | var formkey = $(this).data('pepperland'), opts = cache[formkey]; 63 | 64 | addInputKey(cache[formkey].$form.find(ruleset.qrysel), ruleset, formkey); 65 | opts.rulesets.splice((typeof index === 'number' ? index : -1), 0, ruleset); 66 | return true; 67 | }; 68 | 69 | api.remove = function (target) { // remove target 70 | var $this = $(this), formkey = $.data(this, 'pepperland'), 71 | opts = cache[formkey], rules, i = 0, len; 72 | 73 | if (!opts) return false; 74 | 75 | rules = opts.rulesets; 76 | len = rules ? rules.length : 0; 77 | for (i; i