I am taking input of date of birth and date of death. Validation required
Validate doesn't work as expected and can't figure out the problem. Please help.
Fiddle code
JS libraries used
Additional methods for validation library
var today = new Date(); var authorValidator = $("#itemAuthorForm").validate({ rules : { dateOfBirth : { required : false, date : true, dateITA : true, dateLessThan : '#expiredDate' }, expiredDate : { required : false, date : true, dateITA : true, dateGreaterThan : "#dateOfBirth" } }, onfocusout : function(element) { if ($(element).val()) { $(element).valid(); } } }); var dateOptionsDOE = { maxDate : today, dateFormat : "dd/mm/yy", changeMonth : true, changeYear : true, onClose : function(selectedDate) { $("#dateOfBirth").datepicker("option", "maxDate", selectedDate); } }; var dateOptionsDOB = { maxDate : today, dateFormat : "dd/mm/yy", changeMonth : true, changeYear : true, onClose : function(selectedDate) { $("#expiredDate").datepicker("option", "minDate", selectedDate); } }; jQuery.validator.addMethod("dateGreaterThan", function(value, element, params) { if ($(params).val() === "") return true; if (!/Invalid|NaN/.test(new Date(value))) { return new Date(value) > new Date($(params).val()); } return isNaN(value) && isNaN($(params).val()) || (Number(value) > Number($(params).val())); }, 'Must be greater than {0}.'); jQuery.validator.addMethod("dateLessThan", function(value, element, params) { if ($(params).val() === "") return true; if (!/Invalid|NaN/.test(new Date(value))) { return new Date(value) < new Date($(params).val()); } return isNaN(value) && isNaN($(params).val()) || (Number(value) < Number($(params).val())); }, 'Must be less than {0}.'); $("#expiredDate").datepicker( $.extend({}, $.datepicker.regional['en-GB'], dateOptionsDOE)); $("#dateOfBirth").datepicker( $.extend({}, $.datepicker.regional['en-GB'], dateOptionsDOB));
This will also checks in leap year. This is pure regex, so it's faster than any lib (also faster than moment.js). But if you gonna use a lot of dates in ur code, I do recommend to use moment.js
var dateRegex = /^(?=\d)(?:(?:31(?!.(?:0?[2469]|11))|(?:30|29)(?!.0?2)|29(?=.0?2.(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00)))(?:\x20|$))|(?:2[0-8]|1\d|0?[1-9]))([-.\/])(?:1[012]|0?[1-9])\1(?:1[6-9]|[2-9]\d)?\d\d(?:(?=\x20\d)\x20|$))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\x20[AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/; console.log(dateRegex.test('21/01/1986'));
http://regexper.com/....
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With