jquery - Allow today's date and future date by comparing with today's date in javascript -
i have entry form in can allow today's date , future date can't allow yesterday , previous days. need validation using java-script on client side.
var futuredate = new date($("#deadline").val()); var today = new date();
this gives me date time. mon nov 04 2013 00:00:00 gmt+0530 (india standard time)
want date compare when user enters today's date entered date : fri apr 05 2013 00:00:00 gmt+0530 (india standard time)
current date new date():fri apr 05 2013 16:53:00 gmt+0530 (india standard time)
i tried using sethours()
worked fine current month, converts date in number creates problem when date entered of previous or before month. example: date entered date 23/3/2013
, today 5/4/2013
considers entered date valid.
var futuredate = new date($("#deadline").val()).sethours(0, 0, 0, 0); var today = new date().sethours(0, 0, 0, 0); if (futuredate >= today) { alert("valid"); } else if (futuredate < today) { alert("invalid"); }
try following, uses little function parse datestring date object before using sethours()
. work if string in format dd/mm/yyyy or dd-mm-yyyy.
var futuredate = parsedate(input).sethours(0, 0, 0, 0); var today = new date().sethours(0, 0, 0, 0); if (futuredate >= today) { alert("valid"); } else if (futuredate < today) { alert("invalid"); } //function return date object date string passed function parsedate(input) { var parts = input.match(/(\d+)/g); return new date(parts[2], (parts[1]-1), parts[0]); }
Comments
Post a Comment