Wednesday, 15 August 2012

javascript - Validate date in dd/mm/yyyy format using JQuery Validate -



javascript - Validate date in dd/mm/yyyy format using JQuery Validate -

i taking input of date of birth , date of death. validation required

date of death should more date of birth date format dd/mm/yyyy dates less or equal today.

validate doesn't work expected , can't figure out problem. please help.

fiddle code

js libraries used

jquery ui calendar/datepicker jquery validate form validation

additional methods 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() === "") homecoming true; if (!/invalid|nan/.test(new date(value))) { homecoming new date(value) > new date($(params).val()); } homecoming isnan(value) && isnan($(params).val()) || (number(value) > number($(params).val())); }, 'must greater {0}.'); jquery.validator.addmethod("datelessthan", function(value, element, params) { if ($(params).val() === "") homecoming true; if (!/invalid|nan/.test(new date(value))) { homecoming new date(value) < new date($(params).val()); } homecoming isnan(value) && isnan($(params).val()) || (number(value) < number($(params).val())); }, 'must less {0}.'); $("#expireddate").datepicker( $.extend({}, $.datepicker.regional['en-gb'], dateoptionsdoe)); $("#dateofbirth").datepicker( $.extend({}, $.datepicker.regional['en-gb'], dateoptionsdob));

you don't need date validator. doesn't back upwards dd/mm/yyyy format, , that's why getting "please come in valid date" message input 13/01/2014. have dateita validator, uses dd/mm/yyyy format need.

just date validator, code dategreaterthan , datelessthan calls new date input string , has same issue parsing dates. can utilize function parse date:

function parsedmy(value) { var date = value.split("/"); var d = parseint(date[0], 10), m = parseint(date[1], 10), y = parseint(date[2], 10); homecoming new date(y, m - 1, d); }

javascript jquery validation date datetime

No comments:

Post a Comment