// Generated by CoffeeScript 1.3.3 /* The MIT License (MIT) Copyright (c) 2013 Thom Porter (www.thomporter.com) 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: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. 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 OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ var Valid8r, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }; window.Valid8r = Valid8r = Valid8r = (function() { Valid8r.prototype.res = { alpha: /^[A-z]+$/, num: /^-?[0-9]+$/, numNonNeg: /^[0-9]+$/, alnum: /^[A-z0-9]+$/, url: /^(([A-z]+):\/\/)?\/?([^\/\.]+\.)*?([^\/\.]+\.[^:\/\s\.]{2,3}(\.[^:\/\s\.]‌​{2,3})?)(:\d+)?($|\/)([^#?\s]+)?(.*?)?(#[\w\-]+)?$/, url_noproto: /^\/?([^:\/\s]+)((\/\w+)*\/)([\w\-\.]+[^#?\s]+)(.*)?(#[\w\-]+)?$/, email_simple: /^[^@]+@[A-z0-9_-]+\.[A-z0-9_.-]{2,}$/, email_default: /^[A-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*@(?:[A-z0-9](?:[A-z0-9-]*[A-z0-9])?\.)+(?:[A-Z]{2}|com|org|net|edu|gov|mil|biz|info|mobi|name|aero|asia|jobs|museum)$/, email_rfc5322: /(?:[A-z0-9!#$%&\'*+\/=?^_`{|}~-]+(?:\.[A-z0-9!#$%&\'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[A-z0-9](?:[A-z0-9-]*[A-z0-9])?\.)+[A-z0-9](?:[A-z0-9-]*[A-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[A-z0-9-]*[A-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/, ipv4: /^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/, ipv6: /^\s*((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?\s*$/ }; function Valid8r(options) { this.validate = __bind(this.validate, this); this.validateOnBlur = __bind(this.validateOnBlur, this); this.submitForm = __bind(this.submitForm, this); var defaults; defaults = { bindToBlur: true, type: "text", callback: function(field, err) {}, customValidators: {}, rules: {}, data: {}, form: '' }; this.numErr = 0; this.options = jQuery.extend(defaults, options); if (this.options.rules) { this.setRules(this.options.rules); } this.callback = this.options.callback; this.options.callback = void 0; this.validatingOnBlur = false; this.submittedOnce = false; if (this.options.form) { this.form = jQuery(options.form); this.form.on('submit', this.submitForm); } } Valid8r.prototype.unload = function() { if (this.form) { return this.form.off('submit', this.submitForm); } }; Valid8r.prototype.submitForm = function(e) { var f, num_err, r, _ref, _this = this; this.validatingOnBlur = false; this.submittedOnce = true; num_err = 0; _ref = this.options.rules; for (f in _ref) { r = _ref[f]; this.validate(f, jQuery(r.selector).val(), function(field, err) { if (err) { num_err++; return _this.callback(field, err); } else { return _this.callback(field); } }); } if (num_err) { e.preventDefault(); } return this.numErr = num_err; }; Valid8r.prototype.numErrors = function() { return this.numErr; }; Valid8r.prototype.setRules = function(rules) { /* @since v0.1.0 - _rules can be a property of the configuration object allowing for other properties to be included, such as _globalConditions */ var _this = this; if (rules._rules) { this.options.globalConditions = rules._globalConditions || {}; this.options.rules = rules._rules; } else { this.options.globalConditions = {}; this.options.rules = rules; } return jQuery.each(this.options.rules, function(f, o) { if (!o.selector) { o.selector = _this.options.rules[f].selector = '#' + f; } if (_this.options.bindToBlur && !o.doNotBind) { if (o.type === 'checks' || o.type === 'radios') { return jQuery(o.selector).on('click', _this.validateOnBlur).data('valid8r_field', f); } else { return jQuery(o.selector).on('blur', _this.validateOnBlur).data('valid8r_field', f); } } }); }; Valid8r.prototype.setCallback = function(cb) { return this.callback = cb; }; Valid8r.prototype.satisfiesConditions = function(r, rule) { var c, sel, v, _ref; c = ((_ref = r.conditions) != null ? _ref[rule.when] : void 0) || this.options.globalConditions[rule.when]; sel = c.selector || '#' + c.field; v = jQuery(sel).val(); return v === c.is; }; Valid8r.prototype.validateOnBlur = function(e) { var f, v; this.validatingOnBlur = true; f = jQuery(e.target).data('valid8r_field'); v = jQuery(e.target).val(); return this.validate(f, v, this.callback); }; Valid8r.prototype.validate = function(field, value, cb) { var num_rules, r, rule, _i, _len, _ref; r = this.options.rules[field]; if (!r) { return cb(field, 'Not Setup in Rules'); } num_rules = 0; _ref = r.rules; for (_i = 0, _len = _ref.length; _i < _len; _i++) { rule = _ref[_i]; if ((rule.async && !this.validatingOnBlur) || (rule.when && !this.satisfiesConditions(r, rule))) { continue; } num_rules++; switch (rule.rule) { case "required": if (!this.validRequired(field, value, rule, cb)) { return false; } break; case "len": if (!this.validLen(field, value, rule, cb)) { return false; } break; case "isAlpha": if (!this.validIsAlpha(field, value, rule, cb)) { return false; } break; case "isNum": if (!this.validIsNum(field, value, rule, cb)) { return false; } break; case "isAlnum": if (!this.validIsAlnum(field, value, rule, cb)) { return false; } break; case "formattedAs": if (!this.validFormat(field, value, rule, cb)) { return false; } break; case "regex": if (!this.validRegex(field, value, rule, cb)) { return false; } break; case "val": if (!this.validVal(field, value, rule, cb)) { return false; } break; case "email": if (!this.validEmail(field, value, rule, cb)) { return false; } break; case "url": if (!this.validUrl(field, value, rule, cb)) { return false; } break; case "checks": if (!this.validChecks(field, r, rule, cb)) { return; } break; case "radios": if (!this.validRadios(field, r, rule, cb)) { return false; } break; case "custom": if (!this.validCustom(field, value, rule, cb)) { return false; } break; case "ip": if (!this.validIp(field, value, rule, cb)) { return false; } break; default: cb(field, 'Invalid Validator: ' + rule.rule); } } if (!num_rules) { return cb(field); } }; Valid8r.prototype.validCustom = function(field, value, rule, cb) { if (rule.func && typeof rule.func === 'function') { return rule.func(field, value, cb); } else if (rule.func && typeof rule.func === 'string') { if (this.options.customValidators[rule.func]) { return this.options.customValidators[rule.func](field, value, cb); } else if (typeof window[rule.func] === 'function') { return window[rule.func](field, value, cb); } else { cb(field, 'Invalid Custom Function: ' + rule.func); return false; } } else { cb(field, 'Invalid Custom Function: ' + rule.func); return false; } }; Valid8r.prototype.validRequired = function(field, value, rule, cb) { if (value === '') { cb(field, rule.errStr || 'Please enter a value.'); return false; } cb(field); return true; }; Valid8r.prototype.validRadios = function(field, parent_rule, rule, cb) { var sel; sel = parent_rule.selector || 'input[name="' + field + '"]'; if (!jQuery(sel + ':checked').length) { cb(field, rule.errStr || 'Please choose a value.'); return false; } cb(field); return true; }; Valid8r.prototype.validIp = function(field, value, rule, cb) { if (value) { if (rule.v) { if (!value.match(this.res['ipv' + rule.v])) { cb(field, rule.errStr || 'Invalid IP Address'); return false; } } else { if (!value.match(this.res.ipv4) && !value.match(this.res.ipv6)) { cb(field, rule.errStr || 'Invalid IP Address'); return false; } } } cb(field); return true; }; Valid8r.prototype.validLen = function(field, value, rule, cb) { var len; len = value.length; if (rule.min && rule.max) { if (len < rule.min || len > rule.max) { cb(field, rule.errStr || 'Between %d and %d characters required.'); return false; } } if (rule.min) { if (value.length < rule.min) { cb(field, rule.errStr || 'At least %d characters are required.'); return false; } } if (rule.max) { if (value.length > rule.max) { cb(field, rule.errStr || 'At least %d characters are required.'); return false; } } cb(field); return true; }; Valid8r.prototype.validIsAlpha = function(field, value, rule, cb) { if (value !== '' && !value.match(this.res.alpha)) { cb(field, rule.errStr || 'Please enter alphabetic characters only (A-z).'); return false; } cb(field); return true; }; Valid8r.prototype.validIsNum = function(field, value, rule, cb) { if (value !== '') { if (rule.nonNeg) { if (!value.match(this.res.numNonNeg)) { cb(field, rule.errStr || 'Please enter numeric characters only (0-9).'); return false; } } else { if (!value.match(this.res.num)) { cb(field, rule.errStr || 'Please enter numeric characters only (0-9).'); return false; } } } cb(field); return true; }; Valid8r.prototype.validIsAlnum = function(field, value, rule, cb) { if (value !== '' && !value.match(this.res.alnum)) { cb(field, rule.errStr || 'Please enter alphanumeric characters only (A-z, 0-9).'); return false; } cb(field); return true; }; Valid8r.prototype.validFormat = function(field, value, rule, cb) { var re; if (value !== '') { re = this.preg_quote(rule.format); re = re.replace(/D/g, '\\d').replace(/A/g, '[a-zA-Z]'); if (!value.match(re)) { cb(field, rule.errStr || 'Does not match required format of: ' + rule.format); return false; } } cb(field); return true; }; Valid8r.prototype.validRegex = function(field, value, rule, cb) { var re; if (value !== '') { re = new RegExp(rule.pattern, rule.modifiers); if (!value.match(re)) { cb(field, rule.errStr || 'Does not match required pattern: ' + rule.pattern); return false; } } cb(field); return true; }; Valid8r.prototype.validVal = function(field, value, rule, cb) { var v; if (value !== '') { v = parseFloat(value); if (v === NaN) { cb(field, rule.nanErrStr || 'Please enter a number.'); return false; } if (rule.is) { if (v !== rule.is) { cb(field, rule.errStr || 'Please enter ' + rule.is); return false; } } if (rule.min) { if (v < rule.min) { cb(field, rule.errStr || 'Please enter a number greater than or equal to ' + rule.min); return false; } } if (rule.max) { if (v > rule.max) { cb(field, rule.errStr || 'Please enter a number less than or equal to ' + rule.max); return false; } } if (rule.outside) { if (v >= rule.outside[0] && v <= rule.outside[1]) { cb(field, rule.errStr || 'Please enter a number outside of ' + rule.outside[0] + '-' + rule.outside[1]); return false; } } } cb(field); return true; }; Valid8r.prototype.validEmail = function(field, value, rule, cb) { var re; if (value !== '') { re = rule.validator || 'default'; if (!this.res['email_' + re]) { cb(field, 'INVALID EMAIL VALIDATOR: ' + re); return false; } if (!value.match(this.res['email_' + re])) { cb(field, rule.errStr || 'Pleaes enter a valid email address.'); return false; } } cb(field); return true; }; Valid8r.prototype.validUrl = function(field, value, rule, cb) { var matches, protos; if (value !== "") { matches = value.match(this.res.url); if (!matches || !matches[4]) { cb(field, rule.errStr || 'Invalid URL, please try again.'); return false; } protos = []; if (rule.protocols) { if (typeof rule.protocols === 'string') { protos = rule.protocols.split(','); } else { protos = rule.protocols; } if (protos.indexOf(matches[2]) === -1) { cb(field, rule.errStr || 'Invalid URL Protocol, please enter one of: ' + protos.join(', ')); return false; } } else if (rule.noProtocols) { if (matches[2]) { cb(field, rule.errStr || 'Pleaes enter a URL without protocols (eg, http://, https://, etc.)'); return false; } } else if (!matches[2]) { cb(field, rule.errStr || 'Pleaes enter a URL with a protocol (eg, http://, https://, etc.)'); return false; } } cb(field); return true; }; Valid8r.prototype.validChecks = function(field, parent_rule, rule, cb) { var num_checked, sel; sel = parent_rule.selector || 'input[name="' + field + '"]:checked'; num_checked = jQuery(sel + ':checked').length; if (rule.min && rule.max) { if (rule.min > num_checked || rule.max < num_checked) { cb(field, rule.errStr || 'Please check between ' + rule.min + ' and ' + rule.max + ' options.'); return false; } } if (rule.min) { if (rule.min > num_checked) { cb(field, rule.errStr || 'Please check at least ' + rule.min + ' options.'); return false; } } if (rule.max) { if (rule.max < num_checked) { cb(field, rule.errStr || 'Please check no more than ' + rule.max + ' options.'); return false; } } cb(field); return true; }; Valid8r.prototype.preg_quote = function(str, delimiter) { /* From: http://phpjs.org/functions + original by: booeyOH + improved by: Ates Goral (http://magnetiq.com) + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) + bugfixed by: Onno Marsman + improved by: Brett Zamir (http://brett-zamir.me) * example 1: preg_quote("$40"); * returns 1: '\$40' * example 2: preg_quote("*RRRING* Hello?"); * returns 2: '\*RRRING\* Hello\?' * example 3: preg_quote("\\.+*?[^]$(){}=!<>|:"); * returns 3: '\\\.\+\*\?\[\^\]\$\(\)\{\}\=\!\<\>\|\:' */ return (str + "").replace(new RegExp("[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\" + (delimiter || "") + "-]", "g"), "\\$&"); }; return Valid8r; })();