/** * @fileoverview * Javascript main code. */ var jqValidator = jqValidator || {}; Validator.prototype.and = function(that) { return new And(this,that); }; Validator.prototype.or = function(that) { return new Or(this,that); }; function range(lb,ub) { return new ValidateRange(lb,ub); }; function not(expr) { return new Not(expr); }; function match(regexp) { return new ValidateRegex(regexp); }; $.fn.checkWith = function(rules, errorslist) { this.submit(function() { var values = {}; var form = {}; var input = $(this).serializeArray(); input.forEach(function(element,index) { form[element.name] = new Field(element.name,values); values[element.name] = element.value; }); rules(form); var errors = input.map(function(elem,index) { return form[elem.name].validate(values); }) .filter(function(res) { return !res.result; }) .map(function(result) { $("
").append(result.message).appendTo(errorslist); return result; }); return errors.length == 0; }); return this; }; var alwaysTrue = {isOk:function() { return {result:true}; }}; function Field(name,values) { this.name = name; this.validator = alwaysTrue; this.values = values || {}; }; Field.prototype = { get value() { return this.values[this.name]; }, validate: function(values) { this.values = values || {}; return this.validator.isOk(this.value); }, must: function(rule) { this.validator = rule; } };