Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Validate date in dd/mm/yyyy format using JQuery Validate [duplicate]

I am taking input of date of birth and date of death. Validation required

  1. date of death should be more than date of birth
  2. Date format to be dd/mm/yyyy
  3. Dates to be less than or equal to today.

Validate doesn't work as expected and can't figure out the problem. Please help.

Fiddle code

JS libraries used

  1. JQuery UI for Calendar/Datepicker
  2. JQuery validate for form validation
  3. 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)); 
like image 238
Cipher Avatar asked Jun 24 '14 07:06

Cipher


Video Answer


1 Answers

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')); 

enter image description here

http://regexper.com/....

like image 130
Leonardo Cavalcante Avatar answered Sep 25 '22 23:09

Leonardo Cavalcante