Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create datecomparison.js #1320

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/extra/validator/datecomparison.js
Original file line number Diff line number Diff line change
@@ -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'
}
});