gpt4 book ai didi

javascript - 通过 javascript 进行日期验证

转载 作者:行者123 更新时间:2023-11-28 16:40:44 25 4
gpt4 key购买 nike

请帮我解决我的问题。我在 javascript 中遇到了一个问题。我的问题是我必须使用日期验证。我有两个日期字段,我对两个日期字段都进行了验证,但问题是它在一个日期字段上工作,而不在另一个日期字段上工作。喜欢:

javascript代码

function ValidateDate(DateFrom,DateTo)
{
var SDate = document.checkbookrequest.txtDateFrom.value;
var EDate = document.checkbookrequest.txtDateTo.value;
//variables for date from.
//In dob1 i am taking the date which the user is giving
//and split it into mm/dd/yyyy to compare with current date
var dob1=document.checkbookrequest.txtDateFrom.value;
var arr1 = dob1.split("/");
var m1= arr1[0];
var d1 = arr1[1];
var y1 = arr1[2];
//variables for date from.
//In dob i am taking the date which the user is giving
//and split it into mm/dd/yyyy to compare with current date
var dob=document.checkbookrequest.txtDateTo.value;
var arr = dob.split("/");
var m= arr[0];
var d = arr[1];
var y = arr[2];

//these variable for checking the datefiels should not be blank
var endDate = new Date(EDate);
var startDate= new Date(SDate);

if(SDate != '' && EDate != '' && startDate > endDate)
{

alert(" Date To must be greater than or equal to Date From.");
document.checkbookrequest.txtDateTo.value = "";
document.checkbookrequest.txtDateTo.focus();
return false;
}
else if(SDate == '')
{
alert("Please enter Date From");
document.checkbookrequest.txtDateFrom.focus();
return false;
}
else if(EDate == '')
{
alert("Please enter Date To");
document.checkbookrequest.txtDateTo.focus();
return false;
}

//this is to chck the from date
else if (EDate != "")
{
currentdate = new Date();
a=currentdate.getMonth()+1;
b=currentdate.getDate();
c=currentdate.getFullYear();
if((d==b)&&(m==a)&&(y==c))
{
alert('Please Enter valid Date Of Birth');
return false;
}
else if ((d>b)&&(m==a)&&(y==c))
{
alert('Please Enter valid Date Of Birth');
return false;
}
else if (y<=c-100)
{
alert('Please Enter valid Date Of Birth');
return false;
}
else{
document.checkbookrequest.cmd.value='btnSearch';
document.checkbookrequest.submit();
}
}

//this is to chck the to date
else if (SDate != "")
{
currentdate = new Date();
a=currentdate.getMonth()+1;
b=currentdate.getDate();
c=currentdate.getFullYear();
if((d1==b)&&(m1==a)&&(y1==c))
{
alert('Please Enter valid Date Of Birth in DateFrom');
return false;
}
else if ((d1>b)&&(m1==a)&&(y1==c))
{
alert('Please Enter valid Date Of Birth in DateFrom');
return false;
}
else if (y1<=c-100)
{
alert('Please Enter valid Date Of Birth in DateFrom');
return false;
}
else{
document.checkbookrequest.cmd.value='btnSearch';
document.checkbookrequest.submit();
}
}


else{
document.checkbookrequest.cmd.value='btnSearch';
document.checkbookrequest.submit();
}
}

html代码

<td>Date From:<font color="red">*</font></td>
<td><input type="text" name="txtDateFrom" ></td>

<td>Date To:<font color="red">*</font></td>
<td><input type="text" name="txtDateTo"> </td>

<input type="button" name="btnSearch" value="Search" onclick="return ValidateDate(txtDateFrom,txtDateTo)" /></td>

如果我将“dateto”验证放在上面,那么它就可以工作,但如果我将它放在日期验证之后,那么日期开始就可以工作,而日期就不能工作

这有什么问题吗?请帮我...提前致谢

最佳答案

我建议您完全放弃这条路,并通过使用无法输入无效数据的控件来减轻您的头痛。你为什么不使用 jQuery calendar用于输入。有了两个有效日期,您的验证代码现在将仅限于检查结束日期是否早于开始日期

关于javascript - 通过 javascript 进行日期验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/838983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com