Javascript Date Validation (Range)
Is it possible through Jquery/Javascript that I can validate a date with the following conditions
example date : 10/10/2011
Conditions : Not in the past (accept today onwards) Not more than 18 months (this can be variable)
Because the ways I found out was
var e = new Date(); 开发者_开发百科
Alert(e.getMonth() + 2);
but this is not what I wanted
you might want to have a look at date.js
it's a great library for manipulating dates in javascript.
http://www.datejs.com/
here's the documentation:
http://code.google.com/p/datejs/wiki/APIDocumentation
it can also do the comparisons for you!
have a look at Date.compare and Date.addMonths
You can use JQuery's Datepicker, it has validation included and is pretty nice overall.
It can be done like this:
var toValidate = new Date('10/10/2011');
var months = 18;
var now = new Date();
var minDate = new Date(now.getFullYear(), now.getMonth(), now.getDate());
var maxDate = new Date(now.getFullYear(), now.getMonth() + months, now.getDate());
var valid = minDate < toValidate && toValidate < maxDate;
精彩评论