/** * @name nFormat * @description validation for input to match given format * @author Joseph Burr * * Ex: nFormat: '##.#' */ (function() { $.validator.addMethod('nFormat', function(value, element, format) { var halves = format.split('.'); var exp = format .replace(/#\./, '[0-9]?.') // Num before . can be 0-9 .replace(/^#(#*)/, '([1-9]$1)?') // If starts with # (that is not immediatley before ., 1-9) .replace(/\.#/, '(\\.[0-9])?') // If num after ., . is required too .replace(/#/g, '[0-9]?'); // Replace all other numbers with optional 0 - 9 var reg = new RegExp('^' + exp + '$'); return value.match(reg); }, function(format) { return 'Value does not match required format ' + format + ' !'; }); })(); /** * @name range * @description validation for input to be within a range * @author Joseph Burr * * Ex: range: [0, 9] // [min, max] */ (function() { $.validator.addMethod('rangeSlider', function(value, element, range) { var o = range.reduce(function(o, e) { var n = e; if (typeof e === 'object') n = e.value; if (n > o.max || o.max === undefined) o.max = +n; if (n < o.min || o.min === undefined) o.min = +n; return o; }, {}); return o.min <= +value && +value <= o.max; }, function(range) { var o = range.reduce(function(o, e) { var n = e; if (typeof e === 'object') n = e.value; if (n > o.max || o.max === undefined) o.max = +n; if (n < o.min || o.min === undefined) o.min = +n; return o; }, {}); return 'Value must be between ' + o.min + ' - ' + o.max + '.'; }); })(); /** * @name render * @description returns HTML form group with appropriate rules bound * @author Joseph Burr * * rules: {// jQuery validation rules} * units: {// Array of units} * } * Ex: renderValidatedInput({ * * }); */ /* function renderValidatedInput(name, opts) { var rules = opts.rules || {}; var units = opts.units || []; var placeholder = name; return '' + '
' + '
' + '' + '' + '
' + '
'; } */