感谢所有关于javascript和比较日期的其他SO问题,我能够想出适合我的情况的东西。现在,如果输入的日期超过30天,我只需要清除输入字段。JavaScript会检查输入的日期是否超过30天,如果有,清除输入字段
function ChngStatusEffective(obj)
{
p=/(\d{1,2})\/(\d{1,2})\/(\d{1,4})/;
if (!obj.value.match(p)) return;
num=new Array();
num=obj.value.match(p);
if (num[1].length == 1) num[1]="0" + num[1];
if (num[2].length == 1) num[2]="0" + num[2];
if (num[3].length == 1) num[3]="200" + num[3];
if (num[3].length == 2) num[3]="20" + num[3];
strValue= num[1] + "/" + num[2] + "/" + num[3];
var today = new Date();
var Date2 = new Date (strValue);
var Days = Math.floor((Date2.getTime() - today.getTime())/(1000*60*60*24));
alert('Days between two dates is: ' + Days);
if (Days >= 0)
{
alert('Future dates are not allowed ' + Days);
// document.getElementByName('statuseffective').value();
$('#stauseffective').val('');
return false;
}
else if ((Days === -1) || (Days > -31))
{
alert('Date entered looks good' + Days);
}
else
{
alert('Are you sure you entered the correct date? ' + Days);
}
validateUSDate(strValue)
}
的函数被调用:
<input type="text" name="statuseffective" onchange="ChngStatusEffective(this)" />
我只需要这个输入字段名,statuseffective
被消隐。 注意:可能有一个输入字段或可能有100个。我只需要一个输入字段空白。
这可能会帮助,以检查是否日期是在一个范围内工作:http://stackoverflow.com/a/1906680/158014 – Jakub 2012-01-27 18:45:23
我的功能本身它在确定工作正常如果输入的日期在范围内,我只需要在有多个的情况下清除正确的输入框。每个输入框可能有不同的日期,因此我无法将它们全部清除。 – HPWD 2012-01-27 18:52:08