From 2845327992a90e5a390244cb28485a8a467f3779 Mon Sep 17 00:00:00 2001 From: SakhriHoussem Date: Wed, 6 Nov 2019 21:03:15 +0100 Subject: [PATCH] Create datecomparison.js create date comparaison validator --- src/extra/validator/datecomparison.js | 54 +++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/extra/validator/datecomparison.js diff --git a/src/extra/validator/datecomparison.js b/src/extra/validator/datecomparison.js new file mode 100644 index 000000000..df79523e7 --- /dev/null +++ b/src/extra/validator/datecomparison.js @@ -0,0 +1,54 @@ +// Load this after Parsley for additional date comparison validators +// Note: comparing with a reference isn't well supported and not recommended. +// import jQuery from 'jquery'; // Remove this line in ES3 + +// date-gt, date-lt extra validators + +// data-parsley-date-gt +window.Parsley + .addValidator('dateGt', { + requirementType: 'string', + validateString: function(value, requirement) { + + // get the values of targeted input as an array + let comparedToDate = $(requirement).val().split('-'); + // get the values of initial input as an array + let comparedDate = value.split('-'); + + let i; + for (i = 0; i < comparedToDate.length; i++) { + if (comparedToDate[i] !== comparedDate[i]) { + return comparedDate[i] > comparedToDate[i]; + } + } + + }, + messages: { + en: 'This date must be far.', + fr: 'Cette date doit ĂȘtre plus loin' + } + }); + +// data-parsley-date-lt +window.Parsley + .addValidator('dateLt', { + requirementType: 'string', + validateString: function(value, requirement) { + + // get the values of targeted input as an array + let comparedToDate = $(requirement).val().split('-'); + // get the values of initial input as an array + let comparedDate = value.split('-'); + + let i; + for (i = 0; i < comparedToDate.length; i++) { + if (comparedToDate[i] !== comparedDate[i]) { + return comparedDate[i] < comparedToDate[i]; + } + } + }, + messages: { + en: 'This date must be near.', + fr: 'Cette date doit ĂȘtre plus proche' + } + }); \ No newline at end of file