Skip to content

Commit 8899550

Browse files
committed
Adding validations for minimum and maximum dates
Changing the date validation function, adding optional attributes min-date and max-date. If informed, it sets an allowed range for the date. It checks following this order: valid date, minimum date, maximum date. Both data-validation-min-date and max-date should be informed using the same format as the field (yyyy-mm-dd as default or any format informed through data-validation-format). English and portuguese locales are ok. The other ones are untranslated.
1 parent 5f138b3 commit 8899550

File tree

21 files changed

+57
-26
lines changed

21 files changed

+57
-26
lines changed

form-validator/jquery.form-validator.js

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1958,6 +1958,8 @@
19581958
badTelephone: 'You have not given a correct phone number',
19591959
badSecurityAnswer: 'You have not given a correct answer to the security question',
19601960
badDate: 'You have not given a correct date',
1961+
badDateBefore: 'The minimum allowed date is ',
1962+
badDateAfter: 'The maximum allowed date is ',
19611963
lengthBadStart: 'The input value must be between ',
19621964
lengthBadEnd: ' characters',
19631965
lengthTooLongStart: 'The input value is longer than ',
@@ -2286,19 +2288,48 @@
22862288

22872289
/*
22882290
* Validate date
2291+
* element attrs (optional, and both must use the same format as the field)
2292+
* data-validation-min-date
2293+
* data-validation-max-date
22892294
*/
22902295
$.formUtils.addValidator({
22912296
name: 'date',
2292-
validatorFunction: function (date, $el, conf) {
2297+
validatorFunction: function (date, $el, conf, lang) {
22932298
var dateFormat = $el.valAttr('format') || conf.dateFormat || 'yyyy-mm-dd',
22942299
addMissingLeadingZeros = $el.valAttr('require-leading-zero') === 'false';
2295-
return $.formUtils.parseDate(date, dateFormat, addMissingLeadingZeros) !== false;
2296-
},
2297-
errorMessage: '',
2298-
errorMessageKey: 'badDate'
2300+
var dateParsed = $.formUtils.parseDate(date, dateFormat, addMissingLeadingZeros);
2301+
//checking date format
2302+
if (dateParsed === false){
2303+
this.errorMessage = lang.badDate;
2304+
return false;
2305+
}
2306+
var dateInput = new Date(dateParsed[0], dateParsed[1] - 1, dateParsed[2]);
2307+
//checking min date, when informed
2308+
var dateMinValue = $el.valAttr('min-date');
2309+
if (dateMinValue !== false && $.formUtils.parseDate(dateMinValue, dateFormat, addMissingLeadingZeros) !== false){
2310+
var dateMinParsed = $.formUtils.parseDate(dateMinValue, dateFormat, addMissingLeadingZeros);
2311+
var dateMin = new Date(dateMinParsed[0], dateMinParsed[1] - 1, dateMinParsed[2]);
2312+
if (dateInput < dateMin){
2313+
this.errorMessage = lang.badDateBefore + dateMinValue;
2314+
return false;
2315+
}
2316+
}
2317+
//checking max date, when informed
2318+
var dateMaxValue = $el.valAttr('max-date');
2319+
if (dateMaxValue !== false && $.formUtils.parseDate(dateMaxValue, dateFormat, addMissingLeadingZeros) !== false){
2320+
var dateMaxParsed = $.formUtils.parseDate(dateMaxValue, dateFormat, addMissingLeadingZeros);
2321+
var dateMax = new Date(dateMaxParsed[0], dateMaxParsed[1] - 1, dateMaxParsed[2]);
2322+
if (dateInput > dateMax){
2323+
this.errorMessage = lang.badDateAfter + dateMaxValue;
2324+
return false;
2325+
}
2326+
}
2327+
return true;
2328+
}
2329+
//errorMessage: '', //setted above on the function
2330+
//errorMessageKey: 'badDate' //not used
22992331
});
23002332

2301-
23022333
/*
23032334
* Validate group of checkboxes, validate qty required is checked
23042335
* written by Steve Wasiura : http://stevewasiura.waztech.com

form-validator/lang/ar.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

form-validator/lang/ca.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

form-validator/lang/cs.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)