From 3e8593c7d72eda252d4058fd725488074ae157bb Mon Sep 17 00:00:00 2001 From: schroeder Date: Mon, 10 Mar 2014 13:57:54 +0100 Subject: [PATCH 01/14] formater for string added --- src/jqBootstrapValidation.js | 78 +++++++++++++++++++++--------------- 1 file changed, 45 insertions(+), 33 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index 79f3048..e1f7e9f 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -1,4 +1,16 @@ (function ($) { + + //String formating + // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery + String.prototype.format = String.prototype.f = function() { + var s = this, + i = arguments.length; + + while (i--) { + s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); + } + return s; + }; var createdElements = []; @@ -125,7 +137,7 @@ // --------------------------------------------------------- if ($this.attr("max") !== undefined || $this.attr("aria-valuemax") !== undefined) { var max = ($this.attr("max") !== undefined ? $this.attr("max") : $this.attr("aria-valuemax")); - message = "Too high: Maximum of '" + max + "'"; + message = "Too high: Maximum of '{0}'".f(max); if ($this.data("validationMaxMessage")) { message = $this.data("validationMaxMessage"); } @@ -159,7 +171,7 @@ // MINLENGTH // --------------------------------------------------------- if ($this.attr("minlength") !== undefined) { - message = "Too short: Minimum of '" + $this.attr("minlength") + "' characters"; + message = "Too short: Minimum of '{0}' characters".f($this.attr("minlength")); if ($this.data("validationMinlengthMessage")) { message = $this.data("validationMinlengthMessage"); } @@ -212,7 +224,7 @@ // MINCHECKED // --------------------------------------------------------- if ($this.attr("minchecked") !== undefined) { - message = "Not enough options checked; Minimum of '" + $this.attr("minchecked") + "' required"; + message = "Not enough options checked; Minimum of '{0}' required".f($this.attr("minchecked")); if ($this.data("validationMincheckedMessage")) { message = $this.data("validationMincheckedMessage"); } @@ -223,7 +235,7 @@ // MAXCHECKED // --------------------------------------------------------- if ($this.attr("maxchecked") !== undefined) { - message = "Too many options checked; Maximum of '" + $this.attr("maxchecked") + "' required"; + message = "Too many options checked; Maximum of '{0}' required".f($this.attr("maxchecked")); if ($this.data("validationMaxcheckedMessage")) { message = $this.data("validationMaxcheckedMessage"); } @@ -329,7 +341,7 @@ var hasOverrideMessage = !!message; var foundValidator = false; if (!message) { - message = "'" + el + "' validation failed "; + message = "'{0}' validation failed ".f(el, el.toLowerCase()); } $.each( @@ -395,7 +407,7 @@ } if (!foundValidator) { - $.error("Cannot find validation info for '" + el + "'"); + $.error("Cannot find validation info for '{0}'".f(el)); } }); @@ -788,8 +800,8 @@ } var message = "Not in the expected format"; - if ($this.data("validation" + name + "Message")) { - message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + message = $this.data("validation{0}Message".f(name)); } result.message = message; @@ -809,8 +821,8 @@ result.regex = regexFromString('[a-zA-Z0-9.!#$%&\u2019*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}'); var message = "Not a valid email address"; - if ($this.data("validation" + name + "Message")) { - message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + message = $this.data("validation{0}Message".f(name)); } result.message = message; @@ -827,8 +839,8 @@ name: "required", init: function ($this, name) { var message = "This is required"; - if ($this.data("validation" + name + "Message")) { - message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + message = $this.data("validation{0}Message".f(name)); } return {message: message, includeEmpty: true}; @@ -844,7 +856,7 @@ match: { name: "match", init: function ($this, name) { - var elementName = $this.data("validation" + name + "Match"); + var elementName = $this.data("validation{0}Match".f(name)); var $form = $this.parents("form").first(); var $element = $form.find("[name=\"" + elementName + "\"]").first(); $element.bind("validation.validation", function () { @@ -865,8 +877,8 @@ message += " '" + $label.first().text() + "'"; } - if ($this.data("validation" + name + "Message")) { - message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + message = $this.data("validation{0}Message".f(name)); } result.message = message; @@ -885,11 +897,11 @@ init: function ($this, name) { var result = {}; - result.max = $this.data("validation" + name + "Max"); + result.max = $this.data("validation{0}Max".f(name)); result.message = "Too high: Maximum of '" + result.max + "'"; - if ($this.data("validation" + name + "Message")) { - result.message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + result.message = $this.data("validation{0}Message".f(name)); } return result; @@ -907,8 +919,8 @@ result.min = $this.data("validation" + name + "Min"); result.message = "Too low: Minimum of '" + result.min + "'"; - if ($this.data("validation" + name + "Message")) { - result.message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + result.message = $this.data("validation{0}Message".f(name)); } return result; @@ -926,8 +938,8 @@ result.maxlength = $this.data("validation" + name + "Maxlength"); result.message = "Too long: Maximum of '" + result.maxlength + "' characters"; - if ($this.data("validation" + name + "Message")) { - result.message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + result.message = $this.data("validation{0}Message".f(name)); } return result; @@ -945,8 +957,8 @@ result.minlength = $this.data("validation" + name + "Minlength"); result.message = "Too short: Minimum of '" + result.minlength + "' characters"; - if ($this.data("validation" + name + "Message")) { - result.message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + result.message = $this.data("validation{0}Message".f(name)); } return result; @@ -997,8 +1009,8 @@ result.minchecked = $this.data("validation" + name + "Minchecked"); var message = "Too few: Min '" + result.minchecked + "' checked"; - if ($this.data("validation" + name + "Message")) { - message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + message = $this.data("validation{0}Message".f(name)); } result.message = message; @@ -1019,24 +1031,24 @@ if ($this.attr("step")) { result.step = $this.attr("step"); } - if ($this.data("validation" + name + "Step")) { - result.step = $this.data("validation" + name + "Step"); + if ($this.data("validation{0}Step".f(name))) { + result.step = $this.data("validation{0}Step".f(name)); } result.decimal = "."; - if ($this.data("validation" + name + "Decimal")) { - result.decimal = $this.data("validation" + name + "Decimal"); + if ($this.data("validation{0}Decimal".f(name))) { + result.decimal = $this.data("validation{0}Decimal".f(name)); } result.thousands = ""; - if ($this.data("validation" + name + "Thousands")) { - result.thousands = $this.data("validation" + name + "Thousands"); + if ($this.data("validation{0}Thousands".f(name))) { + result.thousands = $this.data("validation{0}Thousands".f(name)); } result.regex = regexFromString("([+-]?\\d+(\\" + result.decimal + "\\d+)?)?"); result.message = "Must be a number"; - var dataMessage = $this.data("validation" + name + "Message"); + var dataMessage = $this.data("validation{0}Message".f(name)); if (dataMessage) { result.message = dataMessage; } From 2d10847a16489c1990c9b85651fe666a2e00eca8 Mon Sep 17 00:00:00 2001 From: schroeder Date: Mon, 10 Mar 2014 14:17:23 +0100 Subject: [PATCH 02/14] message strings using formater --- src/jqBootstrapValidation.js | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index e1f7e9f..f46308e 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -3,8 +3,7 @@ //String formating // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery String.prototype.format = String.prototype.f = function() { - var s = this, - i = arguments.length; + var s = this, i = arguments.length; while (i--) { s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); @@ -149,7 +148,7 @@ // --------------------------------------------------------- if ($this.attr("min") !== undefined || $this.attr("aria-valuemin") !== undefined) { var min = ($this.attr("min") !== undefined ? $this.attr("min") : $this.attr("aria-valuemin")); - message = "Too low: Minimum of '" + min + "'"; + message = "Too low: Minimum of '{0}'".f(min); if ($this.data("validationMinMessage")) { message = $this.data("validationMinMessage"); } @@ -160,7 +159,7 @@ // MAXLENGTH // --------------------------------------------------------- if ($this.attr("maxlength") !== undefined) { - message = "Too long: Maximum of '" + $this.attr("maxlength") + "' characters"; + message = "Too long: Maximum of '{0}' characters".f($this.attr("maxlength")); if ($this.data("validationMaxlengthMessage")) { message = $this.data("validationMaxlengthMessage"); } @@ -681,8 +680,8 @@ }; var message = "Not valid"; - if ($this.data("validation" + name + "Message")) { - message = $this.data("validation" + name + "Message"); + if ($this.data("validation{0}Message".f(name))) { + message = $this.data("validation{0}Message".f(name)); } result.message = message; @@ -796,7 +795,7 @@ var regexString = $this.data("validation" + name + "Regex"); result.regex = regexFromString(regexString); if (regexString === undefined) { - $.error("Can't find regex for '" + name + "' validator on '" + $this.attr("name") + "'"); + $.error("Can't find regex for '{0}' validator on '{1}'".f(name, $this.attr("name"))); } var message = "Not in the expected format"; @@ -872,9 +871,9 @@ var message = "Must match"; var $label = null; if (($label = $form.find("label[for=\"" + elementName + "\"]")).length) { - message += " '" + $label.text() + "'"; + message += " '{0}'".f($label.text()); } else if (($label = $element.parents(".control-group").first().find("label")).length) { - message += " '" + $label.first().text() + "'"; + message += " '{0}'".f($label.first().text()); } if ($this.data("validation{0}Message".f(name))) { @@ -899,7 +898,7 @@ result.max = $this.data("validation{0}Max".f(name)); - result.message = "Too high: Maximum of '" + result.max + "'"; + result.message = "Too high: Maximum of '{0}'".f(result.max); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -918,7 +917,7 @@ result.min = $this.data("validation" + name + "Min"); - result.message = "Too low: Minimum of '" + result.min + "'"; + result.message = "Too low: Minimum of '{0}'".f(result.min); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -937,7 +936,7 @@ result.maxlength = $this.data("validation" + name + "Maxlength"); - result.message = "Too long: Maximum of '" + result.maxlength + "' characters"; + result.message = "Too long: Maximum of '{0}' characters".f(result.maxlength); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -956,7 +955,7 @@ result.minlength = $this.data("validation" + name + "Minlength"); - result.message = "Too short: Minimum of '" + result.minlength + "' characters"; + result.message = "Too short: Minimum of '{0}' characters".f(result.minlength); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -981,7 +980,7 @@ result.elements = elements; result.maxchecked = $this.data("validation" + name + "Maxchecked"); - var message = "Too many: Max '" + result.maxchecked + "' checked"; + var message = "Too many: Max '{0}' checked".f(result.maxchecked); if ($this.data("validation" + name + "Message")) { message = $this.data("validation" + name + "Message"); } @@ -1008,7 +1007,7 @@ result.elements = elements; result.minchecked = $this.data("validation" + name + "Minchecked"); - var message = "Too few: Min '" + result.minchecked + "' checked"; + var message = "Too few: Min '{0}' checked".f(result.minchecked); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } From ca304d3ff1ee0a1a4f43231e7a4eca00143d92ba Mon Sep 17 00:00:00 2001 From: Mario-S Date: Tue, 11 Mar 2014 11:59:30 +0100 Subject: [PATCH 03/14] some constants for messages extracted --- src/jqBootstrapValidation.js | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index f46308e..f9ac8c9 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -1,5 +1,9 @@ (function ($) { + var MSG_MAXIMUM = "Too high: Maximum of '{0}'"; + var MSG_MINIMUM = "Too low: Minimum of '{0}'"; + var MSG_TOO_LONG = "Too long: Maximum of '{0}' characters"; + //String formating // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery String.prototype.format = String.prototype.f = function() { @@ -136,7 +140,7 @@ // --------------------------------------------------------- if ($this.attr("max") !== undefined || $this.attr("aria-valuemax") !== undefined) { var max = ($this.attr("max") !== undefined ? $this.attr("max") : $this.attr("aria-valuemax")); - message = "Too high: Maximum of '{0}'".f(max); + message = (MSG_MAXIMUM + "").f(max); if ($this.data("validationMaxMessage")) { message = $this.data("validationMaxMessage"); } @@ -148,7 +152,7 @@ // --------------------------------------------------------- if ($this.attr("min") !== undefined || $this.attr("aria-valuemin") !== undefined) { var min = ($this.attr("min") !== undefined ? $this.attr("min") : $this.attr("aria-valuemin")); - message = "Too low: Minimum of '{0}'".f(min); + message = (MSG_MINIMUM + "").f(min); if ($this.data("validationMinMessage")) { message = $this.data("validationMinMessage"); } @@ -159,7 +163,7 @@ // MAXLENGTH // --------------------------------------------------------- if ($this.attr("maxlength") !== undefined) { - message = "Too long: Maximum of '{0}' characters".f($this.attr("maxlength")); + message = (MSG_TOO_LONG + "").f($this.attr("maxlength")); if ($this.data("validationMaxlengthMessage")) { message = $this.data("validationMaxlengthMessage"); } @@ -898,7 +902,7 @@ result.max = $this.data("validation{0}Max".f(name)); - result.message = "Too high: Maximum of '{0}'".f(result.max); + result.message = MSG_MAXIMUM.f(result.max); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -917,7 +921,7 @@ result.min = $this.data("validation" + name + "Min"); - result.message = "Too low: Minimum of '{0}'".f(result.min); + result.message = MSG_MINIMUM.f(result.min); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -936,7 +940,7 @@ result.maxlength = $this.data("validation" + name + "Maxlength"); - result.message = "Too long: Maximum of '{0}' characters".f(result.maxlength); + result.message = MSG_TOO_LONG.f(result.maxlength); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } From f31869841609175a17aaaa080148997f50970417 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Tue, 11 Mar 2014 14:12:43 +0100 Subject: [PATCH 04/14] some constants for messages extracted --- src/jqBootstrapValidation.js | 33 ++++++++++++++++++++------------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index f9ac8c9..72a2111 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -3,6 +3,13 @@ var MSG_MAXIMUM = "Too high: Maximum of '{0}'"; var MSG_MINIMUM = "Too low: Minimum of '{0}'"; var MSG_TOO_LONG = "Too long: Maximum of '{0}' characters"; + var MSG_TOO_SHORT = "Too short: Minimum of '{0}' characters"; + var MSG_TOO_MANY = "Too many: Max '{0}' checked"; + var MSG_TOO_FEW = "Too few: Min '{0}' checked"; + var MSG_INVALID_EMAIL = "Not a valid email address"; + var MSG_INVALID_FORMAT = "Not in the expected format"; + var MSG_REQUIRED = "This is required"; + var MSG_MUST_NUMBER = "Must be a number"; //String formating // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery @@ -128,7 +135,7 @@ $this.attr("pattern", $this.data("validationPatternPattern")); } if ($this.attr("pattern") !== undefined) { - message = "Not in the expected format"; + message = MSG_INVALID_FORMAT + ""; if ($this.data("validationPatternMessage")) { message = $this.data("validationPatternMessage"); } @@ -174,7 +181,7 @@ // MINLENGTH // --------------------------------------------------------- if ($this.attr("minlength") !== undefined) { - message = "Too short: Minimum of '{0}' characters".f($this.attr("minlength")); + message = MSG_TOO_SHORT + "".f($this.attr("minlength")); if ($this.data("validationMinlengthMessage")) { message = $this.data("validationMinlengthMessage"); } @@ -217,7 +224,7 @@ // EMAIL // --------------------------------------------------------- if ($this.attr("type") !== undefined && $this.attr("type").toLowerCase() === "email") { - message = "Not a valid email address"; + message = MSG_INVALID_EMAIL + ""; if ($this.data("validationEmailMessage")) { message = $this.data("validationEmailMessage"); } @@ -802,7 +809,7 @@ $.error("Can't find regex for '{0}' validator on '{1}'".f(name, $this.attr("name"))); } - var message = "Not in the expected format"; + var message = MSG_INVALID_FORMAT; if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -823,7 +830,7 @@ var result = {}; result.regex = regexFromString('[a-zA-Z0-9.!#$%&\u2019*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}'); - var message = "Not a valid email address"; + var message = MSG_INVALID_EMAIL; if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -841,7 +848,7 @@ required: { name: "required", init: function ($this, name) { - var message = "This is required"; + var message = MSG_REQUIRED; if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -959,7 +966,7 @@ result.minlength = $this.data("validation" + name + "Minlength"); - result.message = "Too short: Minimum of '{0}' characters".f(result.minlength); + result.message = MSG_TOO_SHORT.f(result.minlength); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -984,7 +991,7 @@ result.elements = elements; result.maxchecked = $this.data("validation" + name + "Maxchecked"); - var message = "Too many: Max '{0}' checked".f(result.maxchecked); + var message = MSG_TOO_MANY.f(result.maxchecked); if ($this.data("validation" + name + "Message")) { message = $this.data("validation" + name + "Message"); } @@ -1011,7 +1018,7 @@ result.elements = elements; result.minchecked = $this.data("validation" + name + "Minchecked"); - var message = "Too few: Min '{0}' checked".f(result.minchecked); + var message = MSG_TOO_FEW.f(result.minchecked); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -1050,7 +1057,7 @@ result.regex = regexFromString("([+-]?\\d+(\\" + result.decimal + "\\d+)?)?"); - result.message = "Must be a number"; + result.message = MSG_MUST_NUMBER; var dataMessage = $this.data("validation{0}Message".f(name)); if (dataMessage) { result.message = dataMessage; @@ -1073,7 +1080,7 @@ var result = !(regexResult && stepResult && typeResult); return result; }, - message: "Must be a number" + message: MSG_MUST_NUMBER } }, builtInValidators: { @@ -1118,7 +1125,7 @@ required: { name: "Required", type: "required", - message: "This is required" + message: MSG_REQUIRED + "" }, checkone: { name: "Checkone", @@ -1135,7 +1142,7 @@ pattern: { name: "Pattern", type: "regex", - message: "Not in expected format" + message: MSG_INVALID_FORMAT } } }; From ff72eb4b90a02cae7c0288bd0533b5d1558b23b2 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Tue, 11 Mar 2014 16:22:03 +0100 Subject: [PATCH 05/14] more constants for messages extracted --- src/jqBootstrapValidation.js | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index 72a2111..6361fa8 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -6,10 +6,19 @@ var MSG_TOO_SHORT = "Too short: Minimum of '{0}' characters"; var MSG_TOO_MANY = "Too many: Max '{0}' checked"; var MSG_TOO_FEW = "Too few: Min '{0}' checked"; + var MSG_INVALID = "Not valid"; var MSG_INVALID_EMAIL = "Not a valid email address"; var MSG_INVALID_FORMAT = "Not in the expected format"; var MSG_REQUIRED = "This is required"; var MSG_MUST_NUMBER = "Must be a number"; + var MSG_MUST_MATCH = "Must match"; + var MSG_PASSWD_MATCH = "Does not match the given password"; + var MSG_NO_DEC_ALLOWED = "No decimal places allowed"; + var MSG_NUM_POSITIV = "Must be a positive number"; + var MSG_NUM_NEGATIV = "Must be a negative number"; + var MSG_CHECK_OPTION = "Check at least one option"; + var MSG_MINCHECKED = "Not enough options checked; Minimum of '{0}' required"; + var MSG_MAXCHECKED = "Too many options checked; Maximum of '{0}' required"; //String formating // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery @@ -234,7 +243,7 @@ // MINCHECKED // --------------------------------------------------------- if ($this.attr("minchecked") !== undefined) { - message = "Not enough options checked; Minimum of '{0}' required".f($this.attr("minchecked")); + message = (MSG_MINCHECKED + "").f($this.attr("minchecked")); if ($this.data("validationMincheckedMessage")) { message = $this.data("validationMincheckedMessage"); } @@ -245,7 +254,7 @@ // MAXCHECKED // --------------------------------------------------------- if ($this.attr("maxchecked") !== undefined) { - message = "Too many options checked; Maximum of '{0}' required".f($this.attr("maxchecked")); + message = (MSG_MAXCHECKED + "").f($this.attr("maxchecked")); if ($this.data("validationMaxcheckedMessage")) { message = $this.data("validationMaxcheckedMessage"); } @@ -690,7 +699,7 @@ lastFinished: true }; - var message = "Not valid"; + var message = MSG_INVALID; if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -879,7 +888,7 @@ $.error("Can't find field '" + elementName + "' to match '" + $this.attr("name") + "' against in '" + name + "' validator"); } - var message = "Must match"; + var message = MSG_MUST_MATCH; var $label = null; if (($label = $form.find("label[for=\"" + elementName + "\"]")).length) { message += " '{0}'".f($label.text()); @@ -1092,7 +1101,7 @@ name: "Passwordagain", type: "match", match: "password", - message: "Does not match the given password" + message: MSG_PASSWD_MATCH + "" }, positive: { name: "Positive", @@ -1108,19 +1117,19 @@ name: "Integer", type: "regex", regex: "[+-]?\\d+", - message: "No decimal places allowed" + message: MSG_NO_DEC_ALLOWED + "" }, positivenumber: { name: "Positivenumber", type: "min", min: 0, - message: "Must be a positive number" + message: MSG_NUM_POSITIV + "" }, negativenumber: { name: "Negativenumber", type: "max", max: 0, - message: "Must be a negative number" + message: MSG_NUM_NEGATIV + "" }, required: { name: "Required", @@ -1131,7 +1140,7 @@ name: "Checkone", type: "minchecked", minchecked: 1, - message: "Check at least one option" + message: MSG_CHECK_OPTION + "" }, number: { name: "Number", From 98cf9e44f23b6efa9ad75f97771504572e24199a Mon Sep 17 00:00:00 2001 From: Mario-S Date: Tue, 11 Mar 2014 16:26:04 +0100 Subject: [PATCH 06/14] more constants for messages extracted --- src/jqBootstrapValidation.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index 6361fa8..48eb9d6 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -19,6 +19,7 @@ var MSG_CHECK_OPTION = "Check at least one option"; var MSG_MINCHECKED = "Not enough options checked; Minimum of '{0}' required"; var MSG_MAXCHECKED = "Too many options checked; Maximum of '{0}' required"; + var MSG_AJAX_FAILED = "ajax call failed"; //String formating // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery @@ -793,7 +794,7 @@ }, failure: function () { validator.lastValid = true; - validator.message = "ajax call failed"; + validator.message = MSG_AJAX_FAILED; validator.lastFinished = true; $this.data("validation" + validator.validatorName + "Message", validator.message); // Timeout is set to avoid problems with the events being considered 'already fired' From c0cfb9bfce9f145a972cf50702198926b7270d22 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Thu, 13 Mar 2014 20:20:41 +0100 Subject: [PATCH 07/14] started working on map for messages --- src/jqBootstrapValidation.js | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index 48eb9d6..d047ab1 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -1,7 +1,9 @@ (function ($) { + + var messages = {}; + messages["max"] = "Too high: Maximum of '{0}'"; + messages["min"] = "Too low: Minimum of '{0}'"; - var MSG_MAXIMUM = "Too high: Maximum of '{0}'"; - var MSG_MINIMUM = "Too low: Minimum of '{0}'"; var MSG_TOO_LONG = "Too long: Maximum of '{0}' characters"; var MSG_TOO_SHORT = "Too short: Minimum of '{0}' characters"; var MSG_TOO_MANY = "Too many: Max '{0}' checked"; @@ -31,6 +33,10 @@ } return s; }; + + function getMessage(key){ + return messages[key]; + } var createdElements = []; @@ -157,7 +163,8 @@ // --------------------------------------------------------- if ($this.attr("max") !== undefined || $this.attr("aria-valuemax") !== undefined) { var max = ($this.attr("max") !== undefined ? $this.attr("max") : $this.attr("aria-valuemax")); - message = (MSG_MAXIMUM + "").f(max); + message = getMessage("max"); + message = (message + "").f(max); if ($this.data("validationMaxMessage")) { message = $this.data("validationMaxMessage"); } @@ -169,7 +176,8 @@ // --------------------------------------------------------- if ($this.attr("min") !== undefined || $this.attr("aria-valuemin") !== undefined) { var min = ($this.attr("min") !== undefined ? $this.attr("min") : $this.attr("aria-valuemin")); - message = (MSG_MINIMUM + "").f(min); + message = getMessage("min"); + message = (message + "").f(min); if ($this.data("validationMinMessage")) { message = $this.data("validationMinMessage"); } @@ -919,7 +927,7 @@ result.max = $this.data("validation{0}Max".f(name)); - result.message = MSG_MAXIMUM.f(result.max); + result.message = messages.max.f(result.max); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -938,7 +946,7 @@ result.min = $this.data("validation" + name + "Min"); - result.message = MSG_MINIMUM.f(result.min); + result.message = messages.min.f(result.min); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } From d82aebe8f431d100ccb13f7739f3769947e364c3 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Mon, 17 Mar 2014 22:06:36 +0100 Subject: [PATCH 08/14] moved all messages to map --- src/jqBootstrapValidation.js | 85 +++++++++++++++++------------------- 1 file changed, 41 insertions(+), 44 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index d047ab1..0284e71 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -3,25 +3,22 @@ var messages = {}; messages["max"] = "Too high: Maximum of '{0}'"; messages["min"] = "Too low: Minimum of '{0}'"; - - var MSG_TOO_LONG = "Too long: Maximum of '{0}' characters"; - var MSG_TOO_SHORT = "Too short: Minimum of '{0}' characters"; - var MSG_TOO_MANY = "Too many: Max '{0}' checked"; - var MSG_TOO_FEW = "Too few: Min '{0}' checked"; - var MSG_INVALID = "Not valid"; - var MSG_INVALID_EMAIL = "Not a valid email address"; - var MSG_INVALID_FORMAT = "Not in the expected format"; - var MSG_REQUIRED = "This is required"; - var MSG_MUST_NUMBER = "Must be a number"; - var MSG_MUST_MATCH = "Must match"; - var MSG_PASSWD_MATCH = "Does not match the given password"; - var MSG_NO_DEC_ALLOWED = "No decimal places allowed"; - var MSG_NUM_POSITIV = "Must be a positive number"; - var MSG_NUM_NEGATIV = "Must be a negative number"; - var MSG_CHECK_OPTION = "Check at least one option"; - var MSG_MINCHECKED = "Not enough options checked; Minimum of '{0}' required"; - var MSG_MAXCHECKED = "Too many options checked; Maximum of '{0}' required"; - var MSG_AJAX_FAILED = "ajax call failed"; + messages["maxlength"] = "Too long: Maximum of '{0}' characters"; + messages["minlength"] = "Too short: Minimum of '{0}' characters"; + messages["maxchecked"] = "Too many: Max '{0}' checked"; + messages["minchecked"] = "Too few: Min '{0}' checked"; + messages["invalid"] = "Not valid"; + messages["email"] = "Not a valid email address"; + messages["pattern"] = "Not in the expected format"; + messages["required"] = "This is required"; + messages["number"] = "Must be a number"; + messages["match"] = "Must match"; + messages["match_passwd"] = "Does not match the given password"; + messages["integer"] = "No decimal places allowed"; + messages["num_positiv"] = "Must be a positive number"; + messages["num_negativ"] = "Must be a negative number"; + messages["option_required"] = "Check at least one option"; + messages["ajax_failed"] = "ajax call failed"; //String formating // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery @@ -151,7 +148,7 @@ $this.attr("pattern", $this.data("validationPatternPattern")); } if ($this.attr("pattern") !== undefined) { - message = MSG_INVALID_FORMAT + ""; + message = getMessage("pattern") + ""; if ($this.data("validationPatternMessage")) { message = $this.data("validationPatternMessage"); } @@ -188,7 +185,7 @@ // MAXLENGTH // --------------------------------------------------------- if ($this.attr("maxlength") !== undefined) { - message = (MSG_TOO_LONG + "").f($this.attr("maxlength")); + message = (getMessage("maxlength") + "").f($this.attr("maxlength")); if ($this.data("validationMaxlengthMessage")) { message = $this.data("validationMaxlengthMessage"); } @@ -199,7 +196,7 @@ // MINLENGTH // --------------------------------------------------------- if ($this.attr("minlength") !== undefined) { - message = MSG_TOO_SHORT + "".f($this.attr("minlength")); + message = (getMessage("minlength") + "").f($this.attr("minlength")); if ($this.data("validationMinlengthMessage")) { message = $this.data("validationMinlengthMessage"); } @@ -242,7 +239,7 @@ // EMAIL // --------------------------------------------------------- if ($this.attr("type") !== undefined && $this.attr("type").toLowerCase() === "email") { - message = MSG_INVALID_EMAIL + ""; + message = getMessage("email") + ""; if ($this.data("validationEmailMessage")) { message = $this.data("validationEmailMessage"); } @@ -252,7 +249,7 @@ // MINCHECKED // --------------------------------------------------------- if ($this.attr("minchecked") !== undefined) { - message = (MSG_MINCHECKED + "").f($this.attr("minchecked")); + message = (getMessage("minchecked") + "").f($this.attr("minchecked")); if ($this.data("validationMincheckedMessage")) { message = $this.data("validationMincheckedMessage"); } @@ -263,7 +260,7 @@ // MAXCHECKED // --------------------------------------------------------- if ($this.attr("maxchecked") !== undefined) { - message = (MSG_MAXCHECKED + "").f($this.attr("maxchecked")); + message = (getMessage("maxchecked") + "").f($this.attr("maxchecked")); if ($this.data("validationMaxcheckedMessage")) { message = $this.data("validationMaxcheckedMessage"); } @@ -708,7 +705,7 @@ lastFinished: true }; - var message = MSG_INVALID; + var message = getMessage("invalid"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -802,7 +799,7 @@ }, failure: function () { validator.lastValid = true; - validator.message = MSG_AJAX_FAILED; + validator.message = getMessage("ajax_failed"); validator.lastFinished = true; $this.data("validation" + validator.validatorName + "Message", validator.message); // Timeout is set to avoid problems with the events being considered 'already fired' @@ -827,7 +824,7 @@ $.error("Can't find regex for '{0}' validator on '{1}'".f(name, $this.attr("name"))); } - var message = MSG_INVALID_FORMAT; + var message = getMessage("pattern"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -848,7 +845,7 @@ var result = {}; result.regex = regexFromString('[a-zA-Z0-9.!#$%&\u2019*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}'); - var message = MSG_INVALID_EMAIL; + var message = getMessage("email"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -866,7 +863,7 @@ required: { name: "required", init: function ($this, name) { - var message = MSG_REQUIRED; + var message = getMessage("required"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -897,7 +894,7 @@ $.error("Can't find field '" + elementName + "' to match '" + $this.attr("name") + "' against in '" + name + "' validator"); } - var message = MSG_MUST_MATCH; + var message = getMessage("match"); var $label = null; if (($label = $form.find("label[for=\"" + elementName + "\"]")).length) { message += " '{0}'".f($label.text()); @@ -965,7 +962,7 @@ result.maxlength = $this.data("validation" + name + "Maxlength"); - result.message = MSG_TOO_LONG.f(result.maxlength); + result.message = getMessage("maxlength").f(result.maxlength); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -984,7 +981,7 @@ result.minlength = $this.data("validation" + name + "Minlength"); - result.message = MSG_TOO_SHORT.f(result.minlength); + result.message = getMessage("minlength").f(result.minlength); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -1009,7 +1006,7 @@ result.elements = elements; result.maxchecked = $this.data("validation" + name + "Maxchecked"); - var message = MSG_TOO_MANY.f(result.maxchecked); + var message = getMessage("maxchecked").f(result.maxchecked); if ($this.data("validation" + name + "Message")) { message = $this.data("validation" + name + "Message"); } @@ -1036,7 +1033,7 @@ result.elements = elements; result.minchecked = $this.data("validation" + name + "Minchecked"); - var message = MSG_TOO_FEW.f(result.minchecked); + var message = getMessage("minchecked").f(result.minchecked); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -1075,7 +1072,7 @@ result.regex = regexFromString("([+-]?\\d+(\\" + result.decimal + "\\d+)?)?"); - result.message = MSG_MUST_NUMBER; + result.message = getMessage("number"); var dataMessage = $this.data("validation{0}Message".f(name)); if (dataMessage) { result.message = dataMessage; @@ -1098,7 +1095,7 @@ var result = !(regexResult && stepResult && typeResult); return result; }, - message: MSG_MUST_NUMBER + message: getMessage("number") } }, builtInValidators: { @@ -1110,7 +1107,7 @@ name: "Passwordagain", type: "match", match: "password", - message: MSG_PASSWD_MATCH + "" + message: getMessage("match_passwd") + "" }, positive: { name: "Positive", @@ -1126,30 +1123,30 @@ name: "Integer", type: "regex", regex: "[+-]?\\d+", - message: MSG_NO_DEC_ALLOWED + "" + message: getMessage("integer") + "" }, positivenumber: { name: "Positivenumber", type: "min", min: 0, - message: MSG_NUM_POSITIV + "" + message: getMessage("num_positiv") + "" }, negativenumber: { name: "Negativenumber", type: "max", max: 0, - message: MSG_NUM_NEGATIV + "" + message: getMessage("num_negativ") + "" }, required: { name: "Required", type: "required", - message: MSG_REQUIRED + "" + message: getMessage("required") + "" }, checkone: { name: "Checkone", type: "minchecked", minchecked: 1, - message: MSG_CHECK_OPTION + "" + message: getMessage("option_required") + "" }, number: { name: "Number", @@ -1160,7 +1157,7 @@ pattern: { name: "Pattern", type: "regex", - message: MSG_INVALID_FORMAT + message: getMessage("pattern") } } }; From 496df6aa13407456d2cf6d0fda06558990e9cac3 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Thu, 20 Mar 2014 16:38:56 +0100 Subject: [PATCH 09/14] renamed function to get property, new function to return custom message --- src/jqBootstrapValidation.js | 87 +++++++++++++++++++----------------- 1 file changed, 45 insertions(+), 42 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index 0284e71..120f39b 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -20,21 +20,6 @@ messages["option_required"] = "Check at least one option"; messages["ajax_failed"] = "ajax call failed"; - //String formating - // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery - String.prototype.format = String.prototype.f = function() { - var s = this, i = arguments.length; - - while (i--) { - s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); - } - return s; - }; - - function getMessage(key){ - return messages[key]; - } - var createdElements = []; var defaults = { @@ -53,6 +38,9 @@ filter: function () { // return $(this).is(":visible"); // only validate elements you can see return true; // validate everything + }, + resource: function(key) { + return messages[key]; } }, methods: { @@ -148,7 +136,7 @@ $this.attr("pattern", $this.data("validationPatternPattern")); } if ($this.attr("pattern") !== undefined) { - message = getMessage("pattern") + ""; + message = settings.options.resource("pattern") + ""; if ($this.data("validationPatternMessage")) { message = $this.data("validationPatternMessage"); } @@ -160,7 +148,7 @@ // --------------------------------------------------------- if ($this.attr("max") !== undefined || $this.attr("aria-valuemax") !== undefined) { var max = ($this.attr("max") !== undefined ? $this.attr("max") : $this.attr("aria-valuemax")); - message = getMessage("max"); + message = settings.options.resource("max"); message = (message + "").f(max); if ($this.data("validationMaxMessage")) { message = $this.data("validationMaxMessage"); @@ -173,7 +161,7 @@ // --------------------------------------------------------- if ($this.attr("min") !== undefined || $this.attr("aria-valuemin") !== undefined) { var min = ($this.attr("min") !== undefined ? $this.attr("min") : $this.attr("aria-valuemin")); - message = getMessage("min"); + message = settings.options.resource("min"); message = (message + "").f(min); if ($this.data("validationMinMessage")) { message = $this.data("validationMinMessage"); @@ -185,7 +173,7 @@ // MAXLENGTH // --------------------------------------------------------- if ($this.attr("maxlength") !== undefined) { - message = (getMessage("maxlength") + "").f($this.attr("maxlength")); + message = (settings.options.resource("maxlength") + "").f($this.attr("maxlength")); if ($this.data("validationMaxlengthMessage")) { message = $this.data("validationMaxlengthMessage"); } @@ -196,7 +184,7 @@ // MINLENGTH // --------------------------------------------------------- if ($this.attr("minlength") !== undefined) { - message = (getMessage("minlength") + "").f($this.attr("minlength")); + message = (prop("minlength") + "").f($this.attr("minlength")); if ($this.data("validationMinlengthMessage")) { message = $this.data("validationMinlengthMessage"); } @@ -239,7 +227,7 @@ // EMAIL // --------------------------------------------------------- if ($this.attr("type") !== undefined && $this.attr("type").toLowerCase() === "email") { - message = getMessage("email") + ""; + message = prop("email") + ""; if ($this.data("validationEmailMessage")) { message = $this.data("validationEmailMessage"); } @@ -249,7 +237,7 @@ // MINCHECKED // --------------------------------------------------------- if ($this.attr("minchecked") !== undefined) { - message = (getMessage("minchecked") + "").f($this.attr("minchecked")); + message = (prop("minchecked") + "").f($this.attr("minchecked")); if ($this.data("validationMincheckedMessage")) { message = $this.data("validationMincheckedMessage"); } @@ -260,7 +248,7 @@ // MAXCHECKED // --------------------------------------------------------- if ($this.attr("maxchecked") !== undefined) { - message = (getMessage("maxchecked") + "").f($this.attr("maxchecked")); + message = (prop("maxchecked") + "").f($this.attr("maxchecked")); if ($this.data("validationMaxcheckedMessage")) { message = $this.data("validationMaxcheckedMessage"); } @@ -705,7 +693,7 @@ lastFinished: true }; - var message = getMessage("invalid"); + var message = prop("invalid"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -799,7 +787,7 @@ }, failure: function () { validator.lastValid = true; - validator.message = getMessage("ajax_failed"); + validator.message = prop("ajax_failed"); validator.lastFinished = true; $this.data("validation" + validator.validatorName + "Message", validator.message); // Timeout is set to avoid problems with the events being considered 'already fired' @@ -824,7 +812,7 @@ $.error("Can't find regex for '{0}' validator on '{1}'".f(name, $this.attr("name"))); } - var message = getMessage("pattern"); + var message = prop("pattern"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -845,7 +833,7 @@ var result = {}; result.regex = regexFromString('[a-zA-Z0-9.!#$%&\u2019*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}'); - var message = getMessage("email"); + var message = prop("email"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -863,7 +851,7 @@ required: { name: "required", init: function ($this, name) { - var message = getMessage("required"); + var message = prop("required"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -894,7 +882,7 @@ $.error("Can't find field '" + elementName + "' to match '" + $this.attr("name") + "' against in '" + name + "' validator"); } - var message = getMessage("match"); + var message = prop("match"); var $label = null; if (($label = $form.find("label[for=\"" + elementName + "\"]")).length) { message += " '{0}'".f($label.text()); @@ -962,7 +950,7 @@ result.maxlength = $this.data("validation" + name + "Maxlength"); - result.message = getMessage("maxlength").f(result.maxlength); + result.message = prop("maxlength").f(result.maxlength); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -981,7 +969,7 @@ result.minlength = $this.data("validation" + name + "Minlength"); - result.message = getMessage("minlength").f(result.minlength); + result.message = prop("minlength").f(result.minlength); if ($this.data("validation{0}Message".f(name))) { result.message = $this.data("validation{0}Message".f(name)); } @@ -1006,7 +994,7 @@ result.elements = elements; result.maxchecked = $this.data("validation" + name + "Maxchecked"); - var message = getMessage("maxchecked").f(result.maxchecked); + var message = prop("maxchecked").f(result.maxchecked); if ($this.data("validation" + name + "Message")) { message = $this.data("validation" + name + "Message"); } @@ -1033,7 +1021,7 @@ result.elements = elements; result.minchecked = $this.data("validation" + name + "Minchecked"); - var message = getMessage("minchecked").f(result.minchecked); + var message = prop("minchecked").f(result.minchecked); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); } @@ -1072,7 +1060,7 @@ result.regex = regexFromString("([+-]?\\d+(\\" + result.decimal + "\\d+)?)?"); - result.message = getMessage("number"); + result.message = prop("number"); var dataMessage = $this.data("validation{0}Message".f(name)); if (dataMessage) { result.message = dataMessage; @@ -1095,7 +1083,7 @@ var result = !(regexResult && stepResult && typeResult); return result; }, - message: getMessage("number") + message: prop("number") } }, builtInValidators: { @@ -1107,7 +1095,7 @@ name: "Passwordagain", type: "match", match: "password", - message: getMessage("match_passwd") + "" + message: prop("match_passwd") + "" }, positive: { name: "Positive", @@ -1123,30 +1111,30 @@ name: "Integer", type: "regex", regex: "[+-]?\\d+", - message: getMessage("integer") + "" + message: prop("integer") + "" }, positivenumber: { name: "Positivenumber", type: "min", min: 0, - message: getMessage("num_positiv") + "" + message: prop("num_positiv") + "" }, negativenumber: { name: "Negativenumber", type: "max", max: 0, - message: getMessage("num_negativ") + "" + message: prop("num_negativ") + "" }, required: { name: "Required", type: "required", - message: getMessage("required") + "" + message: prop("required") + "" }, checkone: { name: "Checkone", type: "minchecked", minchecked: 1, - message: getMessage("option_required") + "" + message: prop("option_required") + "" }, number: { name: "Number", @@ -1157,7 +1145,7 @@ pattern: { name: "Pattern", type: "regex", - message: getMessage("pattern") + message: prop("pattern") } } }; @@ -1229,6 +1217,21 @@ } return context[func].apply(context, args); } + + //String formating + // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery + String.prototype.format = String.prototype.f = function() { + var s = this, i = arguments.length; + + while (i--) { + s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); + } + return s; + }; + + function prop(key){ + return messages[key]; + } $.fn.jqBootstrapValidation = function (method) { From 30598157fba9d22c4fab9cd3f799c4e4a6692bc1 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Fri, 21 Mar 2014 08:58:50 +0100 Subject: [PATCH 10/14] added function to pass customized messages --- src/jqBootstrapValidation.js | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index 120f39b..b169b3c 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -40,7 +40,7 @@ return true; // validate everything }, resource: function(key) { - return messages[key]; + return ""; } }, methods: { @@ -136,7 +136,7 @@ $this.attr("pattern", $this.data("validationPatternPattern")); } if ($this.attr("pattern") !== undefined) { - message = settings.options.resource("pattern") + ""; + message = msg(settings.options.resource, "pattern") + ""; if ($this.data("validationPatternMessage")) { message = $this.data("validationPatternMessage"); } @@ -148,7 +148,7 @@ // --------------------------------------------------------- if ($this.attr("max") !== undefined || $this.attr("aria-valuemax") !== undefined) { var max = ($this.attr("max") !== undefined ? $this.attr("max") : $this.attr("aria-valuemax")); - message = settings.options.resource("max"); + message = msg(settings.options.resource, "max"); message = (message + "").f(max); if ($this.data("validationMaxMessage")) { message = $this.data("validationMaxMessage"); @@ -161,7 +161,7 @@ // --------------------------------------------------------- if ($this.attr("min") !== undefined || $this.attr("aria-valuemin") !== undefined) { var min = ($this.attr("min") !== undefined ? $this.attr("min") : $this.attr("aria-valuemin")); - message = settings.options.resource("min"); + message = msg(settings.options.resource, "min"); message = (message + "").f(min); if ($this.data("validationMinMessage")) { message = $this.data("validationMinMessage"); @@ -173,7 +173,7 @@ // MAXLENGTH // --------------------------------------------------------- if ($this.attr("maxlength") !== undefined) { - message = (settings.options.resource("maxlength") + "").f($this.attr("maxlength")); + message = (prop("maxlength") + "").f($this.attr("maxlength")); if ($this.data("validationMaxlengthMessage")) { message = $this.data("validationMaxlengthMessage"); } @@ -184,7 +184,7 @@ // MINLENGTH // --------------------------------------------------------- if ($this.attr("minlength") !== undefined) { - message = (prop("minlength") + "").f($this.attr("minlength")); + message = (msg(settings.options.resource, "minlength") + "").f($this.attr("minlength")); if ($this.data("validationMinlengthMessage")) { message = $this.data("validationMinlengthMessage"); } @@ -227,7 +227,7 @@ // EMAIL // --------------------------------------------------------- if ($this.attr("type") !== undefined && $this.attr("type").toLowerCase() === "email") { - message = prop("email") + ""; + message = msg(settings.options.resource, "email") + ""; if ($this.data("validationEmailMessage")) { message = $this.data("validationEmailMessage"); } @@ -237,7 +237,7 @@ // MINCHECKED // --------------------------------------------------------- if ($this.attr("minchecked") !== undefined) { - message = (prop("minchecked") + "").f($this.attr("minchecked")); + message = (msg(settings.options.resource, "minchecked") + "").f($this.attr("minchecked")); if ($this.data("validationMincheckedMessage")) { message = $this.data("validationMincheckedMessage"); } @@ -248,7 +248,7 @@ // MAXCHECKED // --------------------------------------------------------- if ($this.attr("maxchecked") !== undefined) { - message = (prop("maxchecked") + "").f($this.attr("maxchecked")); + message = (msg(settings.options.resource, "maxchecked") + "").f($this.attr("maxchecked")); if ($this.data("validationMaxcheckedMessage")) { message = $this.data("validationMaxcheckedMessage"); } @@ -1229,6 +1229,14 @@ return s; }; + function msg(resources, key){ + var res = resources(key); + if(res === ""){ + res = prop(key); + } + return res; + } + function prop(key){ return messages[key]; } From 0ff19c1d386e6bbc652680d05667f3200282d2bb Mon Sep 17 00:00:00 2001 From: Mario-S Date: Sun, 23 Mar 2014 15:25:19 +0100 Subject: [PATCH 11/14] added test for issue 21 --- src/jqBootstrapValidation.js | 3 +- test/issues/21/21.html | 32 ++++++ test/issues/21/helpers.js | 161 +++++++++++++++++++++++++++++ test/issues/21/test.js | 92 +++++++++++++++++ test/jqBootstrapValidation_test.js | 2 +- 5 files changed, 288 insertions(+), 2 deletions(-) create mode 100644 test/issues/21/21.html create mode 100644 test/issues/21/helpers.js create mode 100644 test/issues/21/test.js diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index b169b3c..6aecf27 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -195,7 +195,8 @@ // REQUIRED // --------------------------------------------------------- if ($this.attr("required") !== undefined || $this.attr("aria-required") !== undefined) { - message = settings.builtInValidators.required.message; +// message = settings.builtInValidators.required.message; + message = msg(settings.options.resource, "required"); if ($this.data("validationRequiredMessage")) { message = $this.data("validationRequiredMessage"); } diff --git a/test/issues/21/21.html b/test/issues/21/21.html new file mode 100644 index 0000000..cde4f3c --- /dev/null +++ b/test/issues/21/21.html @@ -0,0 +1,32 @@ + + + + + jqBootstrapValidation Test Suite + + + + + + + + + + + + + +

jqBootstrapValidation Test Suite

+

+
+

+
    +
    + lame test markup + normal test markup + awesome test markup +
    + + \ No newline at end of file diff --git a/test/issues/21/helpers.js b/test/issues/21/helpers.js new file mode 100644 index 0000000..1e059b5 --- /dev/null +++ b/test/issues/21/helpers.js @@ -0,0 +1,161 @@ +/*global QUnit:false, module:false, test:false, asyncTest:false, expect:false, console:false*/ +/*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/ +/*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/ +/*global importFromTd:false */ +(function ($) { + window.number_of_submit_successes = 0; + window.number_of_submit_errors = 0; + + window.attachJqbv = function() { + $("#qunit-fixture").find("input,select,textarea").not("[type=submit]").jqBootstrapValidation( + { + preventSubmit: true, + submitError: function($form, event, errors) { + // Here I do nothing, but you could do something like display + // the error messages to the user, log, etc. + window.number_of_submit_errors++; + }, + submitSuccess: function($form, event) { + window.number_of_submit_successes++; + event.preventDefault(); + }, + resource: function(key) { + return "foo"; + } + } + ); + }; + + window.importFromTd = function($td) { + + // Handle single items simply + var result = $td.text(); + if (result.length > 0) { + result = [result]; + } else { + // literally nothing there? Guess it should be empty. + result = []; + } + + // if multiple items, expect them in a list + if ($td.find("ul,ol").length) { + result = $td.find("ol,ul").first().find("li").map(function(i, el) { + return $(el).text(); + }).toArray(); + } + + return result; + }; + + window.arraysMatch = function(first, second) { + return ( + $(first).not(second).length === 0 && + $(second).not(first).length === 0 + ); + }; + + window.numInJQBVTest = 9; + + window.jqbvTestQueue = []; + + window.extractEvents = function($input) { + var eventsArray = null; + if ($input.data("events")) { + eventsArray = $input.data("events"); + } else if ($input._data && $input._data("events")) { + eventsArray = $input._data("events"); + } + return eventsArray; + }; + + window.runJQBVTest = function(value, classChange, classSubmit, messageChange, messageSubmit) { + + var $input = $("#qunit-fixture").find("[name=input]"); + var $controlGroup = $($input.parents(".control-group")[0]); + var $form = $input.parents("form").first(); + var isMulti = ($input.length > 1); + + var values; + if (isMulti) { + if (value.length) { + if (typeof value === "string") { + values = value.split(","); + } else { + // is an array already, so just use it + values = value; + } + } else { + values = []; + } + } else { + values = [value]; + } + + var valueJson = JSON.stringify(values); + + + var valueAccepted = true; + + if (isMulti) { + // dealing with checkboxes, radioboxes, etc + var $inputs = $input; + $inputs.removeAttr("checked"); + $(values).each(function(i, el) { + var $curInput = $inputs.filter("[value=\"" + el + "\"]"); + + if ($curInput.length === 0) { + valueAccepted = false; + } else { + $curInput.attr("checked", "checked"); + } + }); + + deepEqual(valueAccepted, true, "value is accepted by browser - " + valueJson); + + } else { + + // dealing with text, selects, etc + $input.val(values[0]); + + deepEqual($input.val(), values[0], "value is accepted by browser - " + valueJson); + } + + $input.trigger("change.validation"); + var changeClassExpected = ["control-group"].concat(classChange); + var changeClassActual = $controlGroup.attr("class").split(" "); + deepEqual(changeClassActual, changeClassExpected, "classes as expected on change - " + valueJson); + + var changeMessageActual = importFromTd($controlGroup.find(".help-block")); + deepEqual(changeMessageActual, messageChange, "message as expected on change - " + valueJson); + + var prevErrors = window.number_of_submit_errors; + var prevSuccess = window.number_of_submit_successes; + $form.trigger("submit"); + var nowErrors = window.number_of_submit_errors; + var nowSuccess = window.number_of_submit_successes; + + var submitClassExpected = ["control-group"].concat(classSubmit); + var submitClassActual = $controlGroup.attr("class").split(" "); + deepEqual(submitClassActual, submitClassExpected, "classes as expected on submit - " + valueJson); + + var submitMessageExpected = messageSubmit; + var submitMessageActual = importFromTd($controlGroup.find(".help-block")); + deepEqual(submitMessageActual, submitMessageExpected, "message as expected on submit - " + valueJson); + + if (classSubmit.indexOf("error") > -1) { + deepEqual(prevErrors + 1, nowErrors, "expect an error to be fired - " + valueJson); + deepEqual(prevSuccess, nowSuccess, "DID NOT expect success to be fired - " + valueJson); + } else { + deepEqual(prevErrors, nowErrors, "DID NOT expect an error to be fired - " + valueJson); + deepEqual(prevSuccess + 1, nowSuccess, "expect success to be fired - " + valueJson); + } + + $input.trigger("change.validation"); + changeClassActual = $controlGroup.attr("class").split(" "); + deepEqual(changeClassActual, changeClassExpected, "classes revert again on change - " + valueJson); + + changeMessageActual = importFromTd($controlGroup.find(".help-block")); + deepEqual(changeMessageActual, messageChange, "message reverts again on change - " + valueJson); + }; + +})(jQuery); \ No newline at end of file diff --git a/test/issues/21/test.js b/test/issues/21/test.js new file mode 100644 index 0000000..6875e18 --- /dev/null +++ b/test/issues/21/test.js @@ -0,0 +1,92 @@ +/*global QUnit:false, module:false, test:false, asyncTest:false, expect:false, console:false*/ +/*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/ +/*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/ +/*global JSON:false */ +/*global runJQBVTest:false, attachJqbv:false, numInJQBVTest:false, startJQBVTestQueue:false, pushJQBVTest:false, extractEvents:false*/ +/*jshint multistr: true */ +(function($) { + + /* + ======== A Handy Little QUnit Reference ======== + http://docs.jquery.com/QUnit + + Test methods: + - expect(numAssertions) + - stop(increment) + - start(decrement) + Test assertions: + - ok(value, [message]) + - equal(actual, expected, [message]) + - notEqual(actual, expected, [message]) + - deepEqual(actual, expected, [message]) + - notDeepEqual(actual, expected, [message]) + - strictEqual(actual, expected, [message]) + - notStrictEqual(actual, expected, [message]) + - raises(block, [expected], [message]) + */ + + module('customized message', { + setup: function() { + $("#qunit-fixture").append("
    \ +
    \ + \ +
    \ + \ +
    \ +
    \ +
    \ + \ +
    \ +
    \ + "); + attachJqbv(); + }, + teardown: function() { + $("#qunit-fixture").empty(); + } + }); + + test('is required', 1 * numInJQBVTest, function() { + runJQBVTest("", [], ["error"], [], ["foo"]); + }); + + + module('inline message', { + setup: function() { + $("#qunit-fixture").append("
    \ +
    \ + \ +
    \ + \ +
    \ +
    \ +
    \ + \ +
    \ +
    \ + "); + attachJqbv(); + }, + teardown: function() { + $("#qunit-fixture").empty(); + } + }); + + test('is required', 1 * numInJQBVTest , function() { + runJQBVTest("", [], ["error"], [], ["bar"]); + }); + +}(jQuery)); diff --git a/test/jqBootstrapValidation_test.js b/test/jqBootstrapValidation_test.js index c025a05..e561247 100644 --- a/test/jqBootstrapValidation_test.js +++ b/test/jqBootstrapValidation_test.js @@ -1564,5 +1564,5 @@ ok(eventsArray["blur"] && eventsArray["blur"].length === 1, "'blur' event added by default"); ok(eventsArray["click"] && eventsArray["click"].length === 1, "'click' event added by default"); }); - + }(jQuery)); From 9643e9c6300d49172c9850ea4b6c99268868be78 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Sun, 23 Mar 2014 15:32:12 +0100 Subject: [PATCH 12/14] removed commented line --- src/jqBootstrapValidation.js | 1 - 1 file changed, 1 deletion(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index 6aecf27..2a8a06d 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -195,7 +195,6 @@ // REQUIRED // --------------------------------------------------------- if ($this.attr("required") !== undefined || $this.attr("aria-required") !== undefined) { -// message = settings.builtInValidators.required.message; message = msg(settings.options.resource, "required"); if ($this.data("validationRequiredMessage")) { message = $this.data("validationRequiredMessage"); From f72bff79619d86e00ed4048a0e9181bb6c93d9e4 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Sun, 23 Mar 2014 21:45:57 +0100 Subject: [PATCH 13/14] cleanup in helper --- test/issues/21/helpers.js | 108 ++++++++++---------------------------- 1 file changed, 27 insertions(+), 81 deletions(-) diff --git a/test/issues/21/helpers.js b/test/issues/21/helpers.js index 1e059b5..45ee7bf 100644 --- a/test/issues/21/helpers.js +++ b/test/issues/21/helpers.js @@ -2,27 +2,27 @@ /*global start:false, stop:false ok:false, equal:false, notEqual:false, deepEqual:false*/ /*global notDeepEqual:false, strictEqual:false, notStrictEqual:false, raises:false*/ /*global importFromTd:false */ -(function ($) { +(function($) { window.number_of_submit_successes = 0; window.number_of_submit_errors = 0; - + window.attachJqbv = function() { $("#qunit-fixture").find("input,select,textarea").not("[type=submit]").jqBootstrapValidation( - { - preventSubmit: true, - submitError: function($form, event, errors) { - // Here I do nothing, but you could do something like display - // the error messages to the user, log, etc. - window.number_of_submit_errors++; - }, - submitSuccess: function($form, event) { - window.number_of_submit_successes++; - event.preventDefault(); - }, - resource: function(key) { - return "foo"; + { + preventSubmit: true, + submitError: function($form, event, errors) { + // Here I do nothing, but you could do something like display + // the error messages to the user, log, etc. + window.number_of_submit_errors++; + }, + submitSuccess: function($form, event) { + window.number_of_submit_successes++; + event.preventDefault(); + }, + resource: function(key) { + return "foo"; + } } - } ); }; @@ -47,78 +47,24 @@ return result; }; - window.arraysMatch = function(first, second) { - return ( - $(first).not(second).length === 0 && - $(second).not(first).length === 0 - ); - }; - window.numInJQBVTest = 9; window.jqbvTestQueue = []; - window.extractEvents = function($input) { - var eventsArray = null; - if ($input.data("events")) { - eventsArray = $input.data("events"); - } else if ($input._data && $input._data("events")) { - eventsArray = $input._data("events"); - } - return eventsArray; - }; - window.runJQBVTest = function(value, classChange, classSubmit, messageChange, messageSubmit) { var $input = $("#qunit-fixture").find("[name=input]"); var $controlGroup = $($input.parents(".control-group")[0]); var $form = $input.parents("form").first(); - var isMulti = ($input.length > 1); - - var values; - if (isMulti) { - if (value.length) { - if (typeof value === "string") { - values = value.split(","); - } else { - // is an array already, so just use it - values = value; - } - } else { - values = []; - } - } else { - values = [value]; - } - - var valueJson = JSON.stringify(values); - - - var valueAccepted = true; - - if (isMulti) { - // dealing with checkboxes, radioboxes, etc - var $inputs = $input; - $inputs.removeAttr("checked"); - $(values).each(function(i, el) { - var $curInput = $inputs.filter("[value=\"" + el + "\"]"); - - if ($curInput.length === 0) { - valueAccepted = false; - } else { - $curInput.attr("checked", "checked"); - } - }); - deepEqual(valueAccepted, true, "value is accepted by browser - " + valueJson); + var values = [value]; - } else { + var valueJson = JSON.stringify(values); - // dealing with text, selects, etc - $input.val(values[0]); + // dealing with text, selects, etc + $input.val(values[0]); - deepEqual($input.val(), values[0], "value is accepted by browser - " + valueJson); - } + deepEqual($input.val(), values[0], "value is accepted by browser - " + valueJson); $input.trigger("change.validation"); var changeClassExpected = ["control-group"].concat(classChange); @@ -133,7 +79,7 @@ $form.trigger("submit"); var nowErrors = window.number_of_submit_errors; var nowSuccess = window.number_of_submit_successes; - + var submitClassExpected = ["control-group"].concat(classSubmit); var submitClassActual = $controlGroup.attr("class").split(" "); deepEqual(submitClassActual, submitClassExpected, "classes as expected on submit - " + valueJson); @@ -141,13 +87,13 @@ var submitMessageExpected = messageSubmit; var submitMessageActual = importFromTd($controlGroup.find(".help-block")); deepEqual(submitMessageActual, submitMessageExpected, "message as expected on submit - " + valueJson); - + if (classSubmit.indexOf("error") > -1) { - deepEqual(prevErrors + 1, nowErrors, "expect an error to be fired - " + valueJson); - deepEqual(prevSuccess, nowSuccess, "DID NOT expect success to be fired - " + valueJson); + deepEqual(prevErrors + 1, nowErrors, "expect an error to be fired - " + valueJson); + deepEqual(prevSuccess, nowSuccess, "DID NOT expect success to be fired - " + valueJson); } else { - deepEqual(prevErrors, nowErrors, "DID NOT expect an error to be fired - " + valueJson); - deepEqual(prevSuccess + 1, nowSuccess, "expect success to be fired - " + valueJson); + deepEqual(prevErrors, nowErrors, "DID NOT expect an error to be fired - " + valueJson); + deepEqual(prevSuccess + 1, nowSuccess, "expect success to be fired - " + valueJson); } $input.trigger("change.validation"); From ebc47dd4a876e711e5298c9b2753dad8cae715f1 Mon Sep 17 00:00:00 2001 From: Mario-S Date: Mon, 24 Mar 2014 22:36:40 +0100 Subject: [PATCH 14/14] moved related functions to top --- src/jqBootstrapValidation.js | 674 +++++++++++++++++------------------ 1 file changed, 337 insertions(+), 337 deletions(-) diff --git a/src/jqBootstrapValidation.js b/src/jqBootstrapValidation.js index 2a8a06d..a8707e0 100644 --- a/src/jqBootstrapValidation.js +++ b/src/jqBootstrapValidation.js @@ -1,5 +1,5 @@ -(function ($) { - +(function($) { + var messages = {}; messages["max"] = "Too high: Maximum of '{0}'"; messages["min"] = "Too low: Minimum of '{0}'"; @@ -19,7 +19,30 @@ messages["num_negativ"] = "Must be a negative number"; messages["option_required"] = "Check at least one option"; messages["ajax_failed"] = "ajax call failed"; - + + function msg(resources, key) { + var res = resources(key); + if (res === "") { + res = prop(key); + } + return res; + } + + function prop(key) { + return messages[key]; + } + + //String formating + // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery + String.prototype.format = String.prototype.f = function() { + var s = this, i = arguments.length; + + while (i--) { + s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); + } + return s; + }; + var createdElements = []; var defaults = { @@ -30,12 +53,12 @@ submitError: false, // function called if there is an error when trying to submit submitSuccess: false, // function called just before a successful submit event is sent to the server semanticallyStrict: false, // set to true to tidy up generated HTML output - removeSuccess : true, + removeSuccess: true, bindEvents: [], autoAdd: { helpBlocks: true }, - filter: function () { + filter: function() { // return $(this).is(":visible"); // only validate elements you can see return true; // validate everything }, @@ -44,7 +67,7 @@ } }, methods: { - init: function (options) { + init: function(options) { // Get a clean copy of the defaults for extending var settings = $.extend(true, {}, defaults); @@ -54,12 +77,12 @@ var $siblingElements = this; var uniqueForms = $.unique( - $siblingElements.map(function () { - return $(this).parents("form")[0]; - }).toArray() - ); + $siblingElements.map(function() { + return $(this).parents("form")[0]; + }).toArray() + ); - $(uniqueForms).bind("submit.validationSubmit", function (e) { + $(uniqueForms).bind("submit.validationSubmit", function(e) { var $form = $(this); var warningsFound = 0; // Get all inputs @@ -67,7 +90,7 @@ var $allControlGroups = $form.find(".control-group"); // Only trigger validation on the ones that actually _have_ validation - var $inputsWithValidators = $allInputs.filter(function () { + var $inputsWithValidators = $allInputs.filter(function() { return $(this).triggerHandler("getValidatorCount.validation") > 0; }); $inputsWithValidators.trigger("submit.validation"); @@ -76,7 +99,7 @@ $allInputs.trigger("validationLostFocus.validation"); // Okay, now check each controlgroup for errors (or warnings) - $allControlGroups.each(function (i, el) { + $allControlGroups.each(function(i, el) { var $controlGroup = $(el); if ($controlGroup.hasClass("warning") || $controlGroup.hasClass("error")) { $controlGroup.removeClass("warning").addClass("error"); @@ -105,14 +128,14 @@ } }); - return this.each(function () { + return this.each(function() { // Get references to everything we're interested in var $this = $(this), - $controlGroup = $this.parents(".control-group").first(), - $helpBlock = $controlGroup.find(".help-block").first(), - $form = $this.parents("form").first(), - validatorNames = []; + $controlGroup = $this.parents(".control-group").first(), + $helpBlock = $controlGroup.find(".help-block").first(), + $form = $this.parents("form").first(), + validatorNames = []; // create message container if not exists if (!$helpBlock.length && settings.options.autoAdd && settings.options.autoAdd.helpBlocks) { @@ -267,7 +290,7 @@ } // Get extra ones defined on the element's data attributes - $.each($this.data(), function (i, el) { + $.each($this.data(), function(i, el) { var parts = i.replace(/([A-Z])/g, ",$1").split(","); if (parts[0] === "validation" && parts[1]) { validatorNames.push(parts[1]); @@ -281,15 +304,15 @@ var validatorNamesToInspect = validatorNames; var newValidatorNamesToInspect = []; - var uppercaseEachValidatorName = function (i, el) { + var uppercaseEachValidatorName = function(i, el) { validatorNames[i] = formatValidatorName(el); }; - var inspectValidators = function (i, el) { + var inspectValidators = function(i, el) { if ($this.data("validation" + el + "Shortcut") !== undefined) { // Are these custom validators? // Pull them out! - $.each($this.data("validation" + el + "Shortcut").split(","), function (i2, el2) { + $.each($this.data("validation" + el + "Shortcut").split(","), function(i2, el2) { newValidatorNamesToInspect.push(el2); }); } else if (settings.builtInValidators[el.toLowerCase()]) { @@ -297,7 +320,7 @@ // Pull it out! var validator = settings.builtInValidators[el.toLowerCase()]; if (validator.type.toLowerCase() === "shortcut") { - $.each(validator.shortcut.split(","), function (i, el) { + $.each(validator.shortcut.split(","), function(i, el) { el = formatValidatorName(el); newValidatorNamesToInspect.push(el); validatorNames.push(el); @@ -348,7 +371,7 @@ var validators = {}; - $.each(validatorNames, function (i, el) { + $.each(validatorNames, function(i, el) { // Set up the 'override' message var message = $this.data("validation" + el + "Message"); var hasOverrideMessage = !!message; @@ -358,30 +381,30 @@ } $.each( - settings.validatorTypes, - function (validatorType, validatorTemplate) { - if (validators[validatorType] === undefined) { - validators[validatorType] = []; - } - if (!foundValidator && $this.data("validation" + el + formatValidatorName(validatorTemplate.name)) !== undefined) { - var initted = validatorTemplate.init($this, el); - if (hasOverrideMessage) { - initted.message = message; + settings.validatorTypes, + function(validatorType, validatorTemplate) { + if (validators[validatorType] === undefined) { + validators[validatorType] = []; } + if (!foundValidator && $this.data("validation" + el + formatValidatorName(validatorTemplate.name)) !== undefined) { + var initted = validatorTemplate.init($this, el); + if (hasOverrideMessage) { + initted.message = message; + } - validators[validatorType].push( - $.extend( - true, - { - name: formatValidatorName(validatorTemplate.name), - message: message - }, - initted - ) - ); - foundValidator = true; + validators[validatorType].push( + $.extend( + true, + { + name: formatValidatorName(validatorTemplate.name), + message: message + }, + initted + ) + ); + foundValidator = true; + } } - } ); if (!foundValidator && settings.builtInValidators[el.toLowerCase()]) { @@ -396,25 +419,25 @@ foundValidator = true; } else { $.each( - settings.validatorTypes, - function (validatorTemplateType, validatorTemplate) { - if (validators[validatorTemplateType] === undefined) { - validators[validatorTemplateType] = []; - } - if (!foundValidator && validatorType === validatorTemplateType.toLowerCase()) { - $this.data( - "validation" + el + formatValidatorName(validatorTemplate.name), - validator[validatorTemplate.name.toLowerCase()] - ); - validators[validatorType].push( - $.extend( - validator, - validatorTemplate.init($this, el) - ) - ); - foundValidator = true; + settings.validatorTypes, + function(validatorTemplateType, validatorTemplate) { + if (validators[validatorTemplateType] === undefined) { + validators[validatorTemplateType] = []; + } + if (!foundValidator && validatorType === validatorTemplateType.toLowerCase()) { + $this.data( + "validation" + el + formatValidatorName(validatorTemplate.name), + validator[validatorTemplate.name.toLowerCase()] + ); + validators[validatorType].push( + $.extend( + validator, + validatorTemplate.init($this, el) + ) + ); + foundValidator = true; + } } - } ); } } @@ -429,98 +452,98 @@ // ============================================================= $helpBlock.data( - "original-contents", - ( - $helpBlock.data("original-contents") ? - $helpBlock.data("original-contents") : - $helpBlock.html() - ) - ); + "original-contents", + ( + $helpBlock.data("original-contents") ? + $helpBlock.data("original-contents") : + $helpBlock.html() + ) + ); $helpBlock.data( - "original-role", - ( - $helpBlock.data("original-role") ? - $helpBlock.data("original-role") : - $helpBlock.attr("role") - ) - ); + "original-role", + ( + $helpBlock.data("original-role") ? + $helpBlock.data("original-role") : + $helpBlock.attr("role") + ) + ); $controlGroup.data( - "original-classes", - ( - $controlGroup.data("original-clases") ? - $controlGroup.data("original-classes") : - $controlGroup.attr("class") - ) - ); + "original-classes", + ( + $controlGroup.data("original-clases") ? + $controlGroup.data("original-classes") : + $controlGroup.attr("class") + ) + ); $this.data( - "original-aria-invalid", - ( - $this.data("original-aria-invalid") ? - $this.data("original-aria-invalid") : - $this.attr("aria-invalid") - ) - ); + "original-aria-invalid", + ( + $this.data("original-aria-invalid") ? + $this.data("original-aria-invalid") : + $this.attr("aria-invalid") + ) + ); // ============================================================= // VALIDATION // ============================================================= $this.bind( - "validation.validation", - function (event, params) { - - var value = getValue($this); - - // Get a list of the errors to apply - var errorsFound = []; - - $.each(validators, function (validatorType, validatorTypeArray) { - if ( - value || // has a truthy value - value.length || // not an empty string - ( // am including empty values - ( - params && - params.includeEmpty - ) || !!settings.validatorTypes[validatorType].includeEmpty - ) || - ( // validator is blocking submit - !!settings.validatorTypes[validatorType].blockSubmit && - params && !!params.submitting - ) - ) { - $.each( - validatorTypeArray, - function (i, validator) { - if (settings.validatorTypes[validatorType].validate($this, value, validator)) { - errorsFound.push(validator.message); - } - } - ); - } - }); + "validation.validation", + function(event, params) { + + var value = getValue($this); + + // Get a list of the errors to apply + var errorsFound = []; + + $.each(validators, function(validatorType, validatorTypeArray) { + if ( + value || // has a truthy value + value.length || // not an empty string + (// am including empty values + ( + params && + params.includeEmpty + ) || !!settings.validatorTypes[validatorType].includeEmpty + ) || + (// validator is blocking submit + !!settings.validatorTypes[validatorType].blockSubmit && + params && !!params.submitting + ) + ) { + $.each( + validatorTypeArray, + function(i, validator) { + if (settings.validatorTypes[validatorType].validate($this, value, validator)) { + errorsFound.push(validator.message); + } + } + ); + } + }); - return errorsFound; - } + return errorsFound; + } ); $this.bind( - "getValidators.validation", - function () { - return validators; - } + "getValidators.validation", + function() { + return validators; + } ); var numValidators = 0; - $.each(validators, function (i, el) { + $.each(validators, function(i, el) { numValidators += el.length; }); - $this.bind("getValidatorCount.validation", function () { + $this.bind("getValidatorCount.validation", function() { return numValidators; }); @@ -528,135 +551,135 @@ // WATCH FOR CHANGES // ============================================================= $this.bind( - "submit.validation", - function () { - return $this.triggerHandler("change.validation", {submitting: true}); - } + "submit.validation", + function() { + return $this.triggerHandler("change.validation", {submitting: true}); + } ); $this.bind( - ( - settings.options.bindEvents.length > 0 ? - settings.options.bindEvents : - [ - "keyup", - "focus", - "blur", - "click", - "keydown", - "keypress", - "change" - ] - ).concat(["revalidate"]).join(".validation ") + ".validation", - function (e, params) { - - var value = getValue($this); - - var errorsFound = []; - - if (params && !!params.submitting) { - $controlGroup.data("jqbvIsSubmitting", true); - } else if (e.type !== "revalidate") { - $controlGroup.data("jqbvIsSubmitting", false); - } - - var formIsSubmitting = !!$controlGroup.data("jqbvIsSubmitting"); + ( + settings.options.bindEvents.length > 0 ? + settings.options.bindEvents : + [ + "keyup", + "focus", + "blur", + "click", + "keydown", + "keypress", + "change" + ] + ).concat(["revalidate"]).join(".validation ") + ".validation", + function(e, params) { + + var value = getValue($this); + + var errorsFound = []; + + if (params && !!params.submitting) { + $controlGroup.data("jqbvIsSubmitting", true); + } else if (e.type !== "revalidate") { + $controlGroup.data("jqbvIsSubmitting", false); + } - $controlGroup.find("input,textarea,select").not('[type=submit]').each(function (i, el) { - var oldCount = errorsFound.length; - $.each($(el).triggerHandler("validation.validation", params) || [], function (j, message) { - errorsFound.push(message); + var formIsSubmitting = !!$controlGroup.data("jqbvIsSubmitting"); + + $controlGroup.find("input,textarea,select").not('[type=submit]').each(function(i, el) { + var oldCount = errorsFound.length; + $.each($(el).triggerHandler("validation.validation", params) || [], function(j, message) { + errorsFound.push(message); + }); + if (errorsFound.length > oldCount) { + $(el).attr("aria-invalid", "true"); + } else { + var original = $this.data("original-aria-invalid"); + $(el).attr("aria-invalid", (original !== undefined ? original : false)); + } }); - if (errorsFound.length > oldCount) { - $(el).attr("aria-invalid", "true"); - } else { - var original = $this.data("original-aria-invalid"); - $(el).attr("aria-invalid", (original !== undefined ? original : false)); - } - }); - $form.find("input,select,textarea").not($this).not("[name=\"" + $this.attr("name") + "\"]").trigger("validationLostFocus.validation"); + $form.find("input,select,textarea").not($this).not("[name=\"" + $this.attr("name") + "\"]").trigger("validationLostFocus.validation"); - errorsFound = $.unique(errorsFound.sort()); + errorsFound = $.unique(errorsFound.sort()); - // Were there any errors? - if (errorsFound.length) { - // Better flag it up as a warning. - $controlGroup.removeClass("success error warning").addClass(formIsSubmitting ? "error" : "warning"); + // Were there any errors? + if (errorsFound.length) { + // Better flag it up as a warning. + $controlGroup.removeClass("success error warning").addClass(formIsSubmitting ? "error" : "warning"); - // How many errors did we find? - if (settings.options.semanticallyStrict && errorsFound.length === 1) { - // Only one? Being strict? Just output it. - $helpBlock.html(errorsFound[0] + - ( settings.options.prependExistingHelpBlock ? $helpBlock.data("original-contents") : "" )); + // How many errors did we find? + if (settings.options.semanticallyStrict && errorsFound.length === 1) { + // Only one? Being strict? Just output it. + $helpBlock.html(errorsFound[0] + + (settings.options.prependExistingHelpBlock ? $helpBlock.data("original-contents") : "")); + } else { + // Multiple? Being sloppy? Glue them together into an UL. + $helpBlock.html("
    • " + errorsFound.join("
    • ") + "
    " + + (settings.options.prependExistingHelpBlock ? $helpBlock.data("original-contents") : "")); + } } else { - // Multiple? Being sloppy? Glue them together into an UL. - $helpBlock.html("
    • " + errorsFound.join("
    • ") + "
    " + - ( settings.options.prependExistingHelpBlock ? $helpBlock.data("original-contents") : "" )); - } - } else { - $controlGroup.removeClass("warning error success"); - if (value.length > 0) { - $controlGroup.addClass("success"); + $controlGroup.removeClass("warning error success"); + if (value.length > 0) { + $controlGroup.addClass("success"); + } + $helpBlock.html($helpBlock.data("original-contents")); } - $helpBlock.html($helpBlock.data("original-contents")); - } - if (e.type === "blur") { - if( settings.options.removeSuccess ){ - $controlGroup.removeClass("success"); + if (e.type === "blur") { + if (settings.options.removeSuccess) { + $controlGroup.removeClass("success"); + } } } - } ); - $this.bind("validationLostFocus.validation", function () { - if( settings.options.removeSuccess ){ + $this.bind("validationLostFocus.validation", function() { + if (settings.options.removeSuccess) { $controlGroup.removeClass("success"); } }); }); }, - destroy: function () { + destroy: function() { return this.each( - function () { + function() { + + var + $this = $(this), + $controlGroup = $this.parents(".control-group").first(), + $helpBlock = $controlGroup.find(".help-block").first(), + $form = $this.parents("form").first(); + + // remove our events + $this.unbind('.validation'); // events are namespaced. + $form.unbind(".validationSubmit"); + // reset help text + $helpBlock.html($helpBlock.data("original-contents")); + // reset classes + $controlGroup.attr("class", $controlGroup.data("original-classes")); + // reset aria + $this.attr("aria-invalid", $this.data("original-aria-invalid")); + // reset role + $helpBlock.attr("role", $this.data("original-role")); + // remove all elements we created + if ($.inArray($helpBlock[0], createdElements) > -1) { + $helpBlock.remove(); + } - var - $this = $(this), - $controlGroup = $this.parents(".control-group").first(), - $helpBlock = $controlGroup.find(".help-block").first(), - $form = $this.parents("form").first(); - - // remove our events - $this.unbind('.validation'); // events are namespaced. - $form.unbind(".validationSubmit"); - // reset help text - $helpBlock.html($helpBlock.data("original-contents")); - // reset classes - $controlGroup.attr("class", $controlGroup.data("original-classes")); - // reset aria - $this.attr("aria-invalid", $this.data("original-aria-invalid")); - // reset role - $helpBlock.attr("role", $this.data("original-role")); - // remove all elements we created - if ($.inArray($helpBlock[0], createdElements) > -1) { - $helpBlock.remove(); } - - } ); }, - collectErrors: function (includeEmpty) { + collectErrors: function(includeEmpty) { var errorMessages = {}; - this.each(function (i, el) { + this.each(function(i, el) { var $el = $(el); var name = $el.attr("name"); var errors = $el.triggerHandler("validation.validation", {includeEmpty: true}); errorMessages[name] = $.extend(true, errors, errorMessages[name]); }); - $.each(errorMessages, function (i, el) { + $.each(errorMessages, function(i, el) { if (el.length === 0) { delete errorMessages[i]; } @@ -665,26 +688,26 @@ return errorMessages; }, - hasErrors: function () { + hasErrors: function() { var errorMessages = []; - this.find('input,select,textarea').add(this).each(function (i, el) { + this.find('input,select,textarea').add(this).each(function(i, el) { errorMessages = errorMessages.concat( - $(el).triggerHandler("getValidators.validation") ? $(el).triggerHandler("validation.validation", {submitting: true}) : [] - ); + $(el).triggerHandler("getValidators.validation") ? $(el).triggerHandler("validation.validation", {submitting: true}) : [] + ); }); return (errorMessages.length > 0); }, - override: function (newDefaults) { + override: function(newDefaults) { defaults = $.extend(true, defaults, newDefaults); } }, validatorTypes: { callback: { name: "callback", - init: function ($this, name) { + init: function($this, name) { var result = { validatorName: name, callback: $this.data("validation" + name + "Callback"), @@ -701,7 +724,7 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { if (validator.lastValue === value && validator.lastFinished) { return !validator.lastValid; } @@ -714,32 +737,32 @@ var rrjqbvValidator = validator; var rrjqbvThis = $this; executeFunctionByName( - validator.callback, - window, - $this, - value, - function (data) { - if (rrjqbvValidator.lastValue === data.value) { - rrjqbvValidator.lastValid = data.valid; - if (data.message) { - rrjqbvValidator.message = data.message; - } - rrjqbvValidator.lastFinished = true; - rrjqbvThis.data( - "validation" + rrjqbvValidator.validatorName + "Message", - rrjqbvValidator.message - ); - - // Timeout is set to avoid problems with the events being considered 'already fired' - setTimeout(function () { - if (!$this.is(":focus") && $this.parents("form").first().data("jqbvIsSubmitting")) { - rrjqbvThis.trigger("blur.validation"); - } else { - rrjqbvThis.trigger("revalidate.validation"); + validator.callback, + window, + $this, + value, + function(data) { + if (rrjqbvValidator.lastValue === data.value) { + rrjqbvValidator.lastValid = data.valid; + if (data.message) { + rrjqbvValidator.message = data.message; } - }, 1); // doesn't need a long timeout, just long enough for the event bubble to burst + rrjqbvValidator.lastFinished = true; + rrjqbvThis.data( + "validation" + rrjqbvValidator.validatorName + "Message", + rrjqbvValidator.message + ); + + // Timeout is set to avoid problems with the events being considered 'already fired' + setTimeout(function() { + if (!$this.is(":focus") && $this.parents("form").first().data("jqbvIsSubmitting")) { + rrjqbvThis.trigger("blur.validation"); + } else { + rrjqbvThis.trigger("revalidate.validation"); + } + }, 1); // doesn't need a long timeout, just long enough for the event bubble to burst + } } - } ); } @@ -749,7 +772,7 @@ }, ajax: { name: "ajax", - init: function ($this, name) { + init: function($this, name) { return { validatorName: name, url: $this.data("validation" + name + "Ajax"), @@ -758,7 +781,7 @@ lastFinished: true }; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { if ("" + validator.lastValue === "" + value && validator.lastFinished === true) { return validator.lastValid === false; } @@ -771,7 +794,7 @@ url: validator.url, data: "value=" + encodeURIComponent(value) + "&field=" + $this.attr("name"), dataType: "json", - success: function (data) { + success: function(data) { if ("" + validator.lastValue === "" + data.value) { validator.lastValid = !!(data.valid); if (data.message) { @@ -780,18 +803,18 @@ validator.lastFinished = true; $this.data("validation" + validator.validatorName + "Message", validator.message); // Timeout is set to avoid problems with the events being considered 'already fired' - setTimeout(function () { + setTimeout(function() { $this.trigger("revalidate.validation"); }, 1); // doesn't need a long timeout, just long enough for the event bubble to burst } }, - failure: function () { + failure: function() { validator.lastValid = true; validator.message = prop("ajax_failed"); validator.lastFinished = true; $this.data("validation" + validator.validatorName + "Message", validator.message); // Timeout is set to avoid problems with the events being considered 'already fired' - setTimeout(function () { + setTimeout(function() { $this.trigger("revalidate.validation"); }, 1); // doesn't need a long timeout, just long enough for the event bubble to burst } @@ -804,7 +827,7 @@ }, regex: { name: "regex", - init: function ($this, name) { + init: function($this, name) { var result = {}; var regexString = $this.data("validation" + name + "Regex"); result.regex = regexFromString(regexString); @@ -822,14 +845,14 @@ result.originalName = name; return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return (!validator.regex.test(value) && !validator.negative) || - (validator.regex.test(value) && validator.negative); + (validator.regex.test(value) && validator.negative); } }, email: { name: "email", - init: function ($this, name) { + init: function($this, name) { var result = {}; result.regex = regexFromString('[a-zA-Z0-9.!#$%&\u2019*+/=?^_`{|}~-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,4}'); @@ -843,14 +866,14 @@ result.originalName = name; return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return (!validator.regex.test(value) && !validator.negative) || - (validator.regex.test(value) && validator.negative); + (validator.regex.test(value) && validator.negative); } }, required: { name: "required", - init: function ($this, name) { + init: function($this, name) { var message = prop("required"); if ($this.data("validation{0}Message".f(name))) { message = $this.data("validation{0}Message".f(name)); @@ -858,21 +881,21 @@ return {message: message, includeEmpty: true}; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return !!( - (value.length === 0 && !validator.negative) || + (value.length === 0 && !validator.negative) || (value.length > 0 && validator.negative) - ); + ); }, blockSubmit: true }, match: { name: "match", - init: function ($this, name) { + init: function($this, name) { var elementName = $this.data("validation{0}Match".f(name)); var $form = $this.parents("form").first(); var $element = $form.find("[name=\"" + elementName + "\"]").first(); - $element.bind("validation.validation", function () { + $element.bind("validation.validation", function() { $this.trigger("revalidate.validation", {submitting: true}); }); var result = {}; @@ -898,16 +921,16 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return (value !== validator.element.val() && !validator.negative) || - (value === validator.element.val() && validator.negative); + (value === validator.element.val() && validator.negative); }, blockSubmit: true, includeEmpty: true }, max: { name: "max", - init: function ($this, name) { + init: function($this, name) { var result = {}; result.max = $this.data("validation{0}Max".f(name)); @@ -919,14 +942,14 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return (parseFloat(value, 10) > parseFloat(validator.max, 10) && !validator.negative) || - (parseFloat(value, 10) <= parseFloat(validator.max, 10) && validator.negative); + (parseFloat(value, 10) <= parseFloat(validator.max, 10) && validator.negative); } }, min: { name: "min", - init: function ($this, name) { + init: function($this, name) { var result = {}; result.min = $this.data("validation" + name + "Min"); @@ -938,14 +961,14 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return (parseFloat(value) < parseFloat(validator.min) && !validator.negative) || - (parseFloat(value) >= parseFloat(validator.min) && validator.negative); + (parseFloat(value) >= parseFloat(validator.min) && validator.negative); } }, maxlength: { name: "maxlength", - init: function ($this, name) { + init: function($this, name) { var result = {}; result.maxlength = $this.data("validation" + name + "Maxlength"); @@ -957,14 +980,14 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return ((value.length > validator.maxlength) && !validator.negative) || - ((value.length <= validator.maxlength) && validator.negative); + ((value.length <= validator.maxlength) && validator.negative); } }, minlength: { name: "minlength", - init: function ($this, name) { + init: function($this, name) { var result = {}; result.minlength = $this.data("validation" + name + "Minlength"); @@ -976,18 +999,18 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return ((value.length < validator.minlength) && !validator.negative) || - ((value.length >= validator.minlength) && validator.negative); + ((value.length >= validator.minlength) && validator.negative); } }, maxchecked: { name: "maxchecked", - init: function ($this, name) { + init: function($this, name) { var result = {}; var elements = $this.parents("form").first().find("[name=\"" + $this.attr("name") + "\"]"); - elements.bind("change.validation click.validation", function () { + elements.bind("change.validation click.validation", function() { $this.trigger("revalidate.validation", {includeEmpty: true}); }); @@ -1002,19 +1025,19 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return (validator.elements.filter(":checked").length > validator.maxchecked && !validator.negative) || - (validator.elements.filter(":checked").length <= validator.maxchecked && validator.negative); + (validator.elements.filter(":checked").length <= validator.maxchecked && validator.negative); }, blockSubmit: true }, minchecked: { name: "minchecked", - init: function ($this, name) { + init: function($this, name) { var result = {}; var elements = $this.parents("form").first().find("[name=\"" + $this.attr("name") + "\"]"); - elements.bind("change.validation click.validation", function () { + elements.bind("change.validation click.validation", function() { $this.trigger("revalidate.validation", {includeEmpty: true}); }); @@ -1029,16 +1052,16 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { return (validator.elements.filter(":checked").length < validator.minchecked && !validator.negative) || - (validator.elements.filter(":checked").length >= validator.minchecked && validator.negative); + (validator.elements.filter(":checked").length >= validator.minchecked && validator.negative); }, blockSubmit: true, includeEmpty: true }, number: { name: "number", - init: function ($this, name) { + init: function($this, name) { var result = {}; result.step = 1; if ($this.attr("step")) { @@ -1068,7 +1091,7 @@ return result; }, - validate: function ($this, value, validator) { + validate: function($this, value, validator) { var globalValue = value.replace(validator.decimal, ".").replace(validator.thousands, ""); var multipliedValue = parseFloat(globalValue); var multipliedStep = parseFloat(validator.step); @@ -1150,19 +1173,19 @@ } }; - var formatValidatorName = function (name) { + var formatValidatorName = function(name) { return name - .toLowerCase() - .replace( - /(^|\s)([a-z])/g, - function (m, p1, p2) { - return p1 + p2.toUpperCase(); - } - ) - ; + .toLowerCase() + .replace( + /(^|\s)([a-z])/g, + function(m, p1, p2) { + return p1 + p2.toUpperCase(); + } + ) + ; }; - var getValue = function ($this) { + var getValue = function($this) { // Extract the value we're talking about var value = null; var type = $this.attr("type"); @@ -1170,7 +1193,7 @@ value = ($this.is(":checked") ? value : ""); var checkboxParent = $this.parents("form").first() || $this.parents(".control-group").first(); if (checkboxParent) { - value = checkboxParent.find("input[name='" + $this.attr("name") + "']:checked").map(function (i, el) { + value = checkboxParent.find("input[name='" + $this.attr("name") + "']:checked").map(function(i, el) { return $(el).val(); }).toArray().join(","); } @@ -1179,7 +1202,7 @@ value = ($('input[name="' + $this.attr("name") + '"]:checked').length > 0 ? $this.val() : ""); var radioParent = $this.parents("form").first() || $this.parents(".control-group").first(); if (radioParent) { - value = radioParent.find("input[name='" + $this.attr("name") + "']:checked").map(function (i, el) { + value = radioParent.find("input[name='" + $this.attr("name") + "']:checked").map(function(i, el) { return $(el).val(); }).toArray().join(","); } @@ -1217,31 +1240,8 @@ } return context[func].apply(context, args); } - - //String formating - // http://stackoverflow.com/questions/1038746/equivalent-of-string-format-in-jquery - String.prototype.format = String.prototype.f = function() { - var s = this, i = arguments.length; - - while (i--) { - s = s.replace(new RegExp('\\{' + i + '\\}', 'gm'), arguments[i]); - } - return s; - }; - - function msg(resources, key){ - var res = resources(key); - if(res === ""){ - res = prop(key); - } - return res; - } - - function prop(key){ - return messages[key]; - } - $.fn.jqBootstrapValidation = function (method) { + $.fn.jqBootstrapValidation = function(method) { if (defaults.methods[method]) { return defaults.methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); @@ -1254,7 +1254,7 @@ }; - $.jqBootstrapValidation = function (options) { + $.jqBootstrapValidation = function(options) { $(":input").not("[type=image],[type=submit]").jqBootstrapValidation.apply(this, arguments); };