From 403f89ad5748cfdad4728c03f05361d71431612b Mon Sep 17 00:00:00 2001 From: Charlie Garrison Date: Sun, 4 Jan 2015 00:59:21 +1100 Subject: [PATCH 1/7] Add `date_dmy` validation rule. --- js/languages/jquery.validationEngine-en.js | 41 +++++++++++++++------- 1 file changed, 29 insertions(+), 12 deletions(-) diff --git a/js/languages/jquery.validationEngine-en.js b/js/languages/jquery.validationEngine-en.js index ce08433..c701450 100644 --- a/js/languages/jquery.validationEngine-en.js +++ b/js/languages/jquery.validationEngine-en.js @@ -106,20 +106,37 @@ }, "date": { // Check if date is valid by leap year - "func": function (field) { - var pattern = new RegExp(/^(\d{4})[\/\-\.](0?[1-9]|1[012])[\/\-\.](0?[1-9]|[12][0-9]|3[01])$/); - var match = pattern.exec(field.val()); - if (match == null) - return false; + "func": function (field) { + var pattern = new RegExp(/^(\d{4})[\/\-\.](0?[1-9]|1[012])[\/\-\.](0?[1-9]|[12][0-9]|3[01])$/); + var match = pattern.exec(field.val()); + if (match == null) + return false; - var year = match[1]; - var month = match[2]*1; - var day = match[3]*1; - var date = new Date(year, month - 1, day); // because months starts from 0. + var year = match[1]; + var month = match[2]*1; + var day = match[3]*1; + var date = new Date(year, month - 1, day); // because months starts from 0. - return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); - }, - "alertText": "* Invalid date, must be in YYYY-MM-DD format" + return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); + }, + "alertText": "* Invalid date, must be in YYYY-MM-DD format" + }, + "date_dmy": { + // Check if date is valid by leap year + "func": function (field) { + var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[\/\-\.](0?[1-9]|1[012])[\/\-\.](\d{4})$/); + var match = pattern.exec(field.val()); + if (match == null) + return false; + + var day = match[1]*1; + var month = match[2]*1; + var year = match[3]; + var date = new Date(year, month - 1, day); // because months starts from 0. + + return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); + }, + "alertText": "* Invalid date, must be in DD-MM-YYYY format" }, "ipv4": { "regex": /^((([01]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))$/, From 72eed8370f2c8da33c7064f88965fe5c7d85fc7c Mon Sep 17 00:00:00 2001 From: Charlie Garrison Date: Sun, 4 Jan 2015 01:01:42 +1100 Subject: [PATCH 2/7] Ensure correct class name for 'prettySelect' fields when hiding prompt. --- js/jquery.validationEngine.js | 2 ++ 1 file changed, 2 insertions(+) diff --git a/js/jquery.validationEngine.js b/js/jquery.validationEngine.js index f8233e4..b29f463 100644 --- a/js/jquery.validationEngine.js +++ b/js/jquery.validationEngine.js @@ -214,6 +214,8 @@ if($(this).is("form") || $(this).hasClass("validationEngineContainer")) { closingtag = "parentForm"+methods._getClassName($(this).attr("id")); + } else if ($(this).is("select") && options.prettySelect && $(this).is(":hidden")) { + closingtag = methods._getClassName(options.usePrefix + $(this).attr('id') + options.useSuffix) +"formError"; } else { closingtag = methods._getClassName($(this).attr("id")) +"formError"; } From b70c813ddc15e6dbe32b876276eef815e21cb0f7 Mon Sep 17 00:00:00 2001 From: Charlie Garrison Date: Sun, 4 Jan 2015 01:02:54 +1100 Subject: [PATCH 3/7] Ensure closing prompt will honour the jqv-prompt-at data attribute if it exists for the field. --- js/jquery.validationEngine.js | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/js/jquery.validationEngine.js b/js/jquery.validationEngine.js index b29f463..b9da173 100644 --- a/js/jquery.validationEngine.js +++ b/js/jquery.validationEngine.js @@ -1767,6 +1767,11 @@ * field */ _closePrompt: function(field) { + if(field.data('jqv-prompt-at') instanceof jQuery ){ + field = field.data('jqv-prompt-at'); + } else if(field.data('jqv-prompt-at')) { + field = $(field.data('jqv-prompt-at')); + } var prompt = methods._getPrompt(field); if (prompt) prompt.fadeTo("fast", 0, function() { From 8659d5d5d1f18532d57d874046457c82c0dbc53a Mon Sep 17 00:00:00 2001 From: Charlie Garrison Date: Sun, 4 Jan 2015 01:19:03 +1100 Subject: [PATCH 4/7] Detab --- js/languages/jquery.validationEngine-en.js | 60 +++++++++++----------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/js/languages/jquery.validationEngine-en.js b/js/languages/jquery.validationEngine-en.js index c701450..cff6547 100644 --- a/js/languages/jquery.validationEngine-en.js +++ b/js/languages/jquery.validationEngine-en.js @@ -105,38 +105,38 @@ "alertText": "* Invalid floating decimal number" }, "date": { - // Check if date is valid by leap year - "func": function (field) { - var pattern = new RegExp(/^(\d{4})[\/\-\.](0?[1-9]|1[012])[\/\-\.](0?[1-9]|[12][0-9]|3[01])$/); - var match = pattern.exec(field.val()); - if (match == null) - return false; - - var year = match[1]; - var month = match[2]*1; - var day = match[3]*1; - var date = new Date(year, month - 1, day); // because months starts from 0. - - return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); - }, - "alertText": "* Invalid date, must be in YYYY-MM-DD format" + // Check if date is valid by leap year + "func": function (field) { + var pattern = new RegExp(/^(\d{4})[\/\-\.](0?[1-9]|1[012])[\/\-\.](0?[1-9]|[12][0-9]|3[01])$/); + var match = pattern.exec(field.val()); + if (match == null) + return false; + + var year = match[1]; + var month = match[2]*1; + var day = match[3]*1; + var date = new Date(year, month - 1, day); // because months starts from 0. + + return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); + }, + "alertText": "* Invalid date, must be in YYYY-MM-DD format" }, "date_dmy": { - // Check if date is valid by leap year - "func": function (field) { - var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[\/\-\.](0?[1-9]|1[012])[\/\-\.](\d{4})$/); - var match = pattern.exec(field.val()); - if (match == null) - return false; - - var day = match[1]*1; - var month = match[2]*1; - var year = match[3]; - var date = new Date(year, month - 1, day); // because months starts from 0. - - return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); - }, - "alertText": "* Invalid date, must be in DD-MM-YYYY format" + // Check if date is valid by leap year + "func": function (field) { + var pattern = new RegExp(/^(0?[1-9]|[12][0-9]|3[01])[\/\-\.](0?[1-9]|1[012])[\/\-\.](\d{4})$/); + var match = pattern.exec(field.val()); + if (match == null) + return false; + + var day = match[1]*1; + var month = match[2]*1; + var year = match[3]; + var date = new Date(year, month - 1, day); // because months starts from 0. + + return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); + }, + "alertText": "* Invalid date, must be in DD-MM-YYYY format" }, "ipv4": { "regex": /^((([01]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))[.]){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))$/, From 7582e3249c22abb93d1a28802dfd8bffa13079b3 Mon Sep 17 00:00:00 2001 From: Charlie Garrison Date: Tue, 17 Mar 2015 19:01:36 +1100 Subject: [PATCH 5/7] Add 'postcodeAU' validation rule. --- js/contrib/other-validations.js | 7 +++++++ js/languages/jquery.validationEngine-en.js | 12 +++++++++--- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/js/contrib/other-validations.js b/js/contrib/other-validations.js index 4ab2634..ab322a9 100644 --- a/js/contrib/other-validations.js +++ b/js/contrib/other-validations.js @@ -12,6 +12,13 @@ "regex": /^([A-PR-UWYZa-pr-uwyz]([0-9]{1,2}|([A-HK-Ya-hk-y][0-9]|[A-HK-Ya-hk-y][0-9]([0-9]|[ABEHMNPRV-Yabehmnprv-y]))|[0-9][A-HJKS-UWa-hjks-uw])\ {0,1}[0-9][ABD-HJLNP-UW-Zabd-hjlnp-uw-z]{2}|([Gg][Ii][Rr]\ 0[Aa][Aa])|([Ss][Aa][Nn]\ {0,1}[Tt][Aa]1)|([Bb][Ff][Pp][Oo]\ {0,1}([Cc]\/[Oo]\ )?[0-9]{1,4})|(([Aa][Ss][Cc][Nn]|[Bb][Bb][Nn][Dd]|[BFSbfs][Ii][Qq][Qq]|[Pp][Cc][Rr][Nn]|[Ss][Tt][Hh][Ll]|[Tt][Dd][Cc][Uu]|[Tt][Kk][Cc][Aa])\ {0,1}1[Zz][Zz]))$/, "alertText": "* Invalid postcode" }; + $.validationEngineLanguage.allRules["postcodeAU"] = { + // Australian postcodes | Accepts 1234 format zipcodes +// "regex": /^\d{4}?$/, + // From: http://www.etl-tools.com/regular-expressions/is-australian-post-code.html + "regex": /^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$/, + "alertText": "* Invalid postcode" + }; $.validationEngineLanguage.allRules["postcodeNL"] = { // NL zip codes | Accepts 1234AA format zipcodes "regex": /^\d{4}[a-zA-Z]{2}?$/, diff --git a/js/languages/jquery.validationEngine-en.js b/js/languages/jquery.validationEngine-en.js index cff6547..3c17eb4 100644 --- a/js/languages/jquery.validationEngine-en.js +++ b/js/languages/jquery.validationEngine-en.js @@ -37,7 +37,7 @@ "alertText": "* Maximum ", "alertText2": " characters allowed" }, - "groupRequired": { + "groupRequired": { "regex": "none", "alertText": "* You must fill one of the following fields", "alertTextCheckboxMultiple": "* Please select an option", @@ -95,6 +95,12 @@ "regex":/^\d{5}$|^\d{5}-\d{4}$/, "alertText":"* Invalid zip format" }, + "postcodeAU": { + // "regex": /^\d{4}?$/, + // From: http://www.etl-tools.com/regular-expressions/is-australian-post-code.html + "regex": /^(0[289][0-9]{2})|([1345689][0-9]{3})|(2[0-8][0-9]{2})|(290[0-9])|(291[0-4])|(7[0-4][0-9]{2})|(7[8-9][0-9]{2})$/, + "alertText": "* Invalid postcode" + }, "integer": { "regex": /^[\-\+]?\d+$/, "alertText": "* Not a valid integer" @@ -118,7 +124,7 @@ var date = new Date(year, month - 1, day); // because months starts from 0. return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); - }, + }, "alertText": "* Invalid date, must be in YYYY-MM-DD format" }, "date_dmy": { @@ -135,7 +141,7 @@ var date = new Date(year, month - 1, day); // because months starts from 0. return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); - }, + }, "alertText": "* Invalid date, must be in DD-MM-YYYY format" }, "ipv4": { From b59edab2f8180ed6de1e6093edce54ac6a1c79d4 Mon Sep 17 00:00:00 2001 From: Charlie Garrison Date: Tue, 17 Mar 2015 19:02:37 +1100 Subject: [PATCH 6/7] Don't show validation error if datepicker is visible. --- js/jquery.validationEngine.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/js/jquery.validationEngine.js b/js/jquery.validationEngine.js index b9da173..9117f04 100644 --- a/js/jquery.validationEngine.js +++ b/js/jquery.validationEngine.js @@ -55,7 +55,7 @@ // delegate fields form.on(options.validationEventTrigger, "["+options.validateAttribute+"*=validate]:not([type=checkbox]):not([type=radio]):not(.datepicker)", methods._onFieldEvent); form.on("click", "["+options.validateAttribute+"*=validate][type=checkbox],["+options.validateAttribute+"*=validate][type=radio]", methods._onFieldEvent); - form.on(options.validationEventTrigger,"["+options.validateAttribute+"*=validate][class*=datepicker]", {"delay": 300}, methods._onFieldEvent); + form.on(options.validationEventTrigger, "["+options.validateAttribute+"*=validate][class*=datepicker]", {"delay": 300}, methods._onFieldEvent); } if (options.autoPositionUpdate) { $(window).bind("resize", { @@ -81,7 +81,7 @@ // unbind fields form.off(options.validationEventTrigger, "["+options.validateAttribute+"*=validate]:not([type=checkbox]):not([type=radio]):not(.datepicker)", methods._onFieldEvent); form.off("click", "["+options.validateAttribute+"*=validate][type=checkbox],["+options.validateAttribute+"*=validate][type=radio]", methods._onFieldEvent); - form.off(options.validationEventTrigger,"["+options.validateAttribute+"*=validate][class*=datepicker]", methods._onFieldEvent); + form.off(options.validationEventTrigger, "["+options.validateAttribute+"*=validate][class*=datepicker]", methods._onFieldEvent); // unbind form.submit form.off("submit", methods._onSubmitEvent); @@ -550,7 +550,11 @@ if(field.hasClass(options.ignoreFieldsWithClass)) return false; - if (!options.validateNonVisibleFields && (field.is(":hidden") && !options.prettySelect || field.parent().is(":hidden"))) + if (!options.validateNonVisibleFields && (field.is(":hidden") && !options.prettySelect || field.parent().is(":hidden"))) + return false; + + + if (field.hasClass('datepicker') && field.data('datepicker') && field.data('datepicker').picker.is(':visible') ) return false; var rulesParsing = field.attr(options.validateAttribute); From 43c47f481c21bfe46eff40a859b57033d5342925 Mon Sep 17 00:00:00 2001 From: Charlie Garrison Date: Thu, 4 Jun 2015 21:31:18 +1000 Subject: [PATCH 7/7] Use UTC dates for comparison To avoid daylight savings changeover creating a one hour difference, which causes the `day` portion to incorrect, and fail validation. Signed-off-by: Charlie Garrison --- js/languages/jquery.validationEngine-en.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/js/languages/jquery.validationEngine-en.js b/js/languages/jquery.validationEngine-en.js index 3c17eb4..f15316c 100644 --- a/js/languages/jquery.validationEngine-en.js +++ b/js/languages/jquery.validationEngine-en.js @@ -121,7 +121,8 @@ var year = match[1]; var month = match[2]*1; var day = match[3]*1; - var date = new Date(year, month - 1, day); // because months starts from 0. + //var date = new Date(year, month - 1, day); // because months starts from 0. + var date = new Date(Date.UTC(year, month - 1, day)); // because months starts from 0. return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); }, @@ -138,8 +139,9 @@ var day = match[1]*1; var month = match[2]*1; var year = match[3]; - var date = new Date(year, month - 1, day); // because months starts from 0. - + //var date = new Date(year, month - 1, day); // because months starts from 0. + var date = new Date(Date.UTC(year, month - 1, day)); // because months starts from 0. + return (date.getFullYear() == year && date.getMonth() == (month - 1) && date.getDate() == day); }, "alertText": "* Invalid date, must be in DD-MM-YYYY format"