开始日期和结束日期验证 [英] Start Date and End Date Validation
本文介绍了开始日期和结束日期验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
开始日期格式为2011年12月23日(dd-MMM-yyyy)
结束日期格式为2011年12月28日(dd-MMM-yyyy)
我想在javascript(客户端)检查验证
开始日期不能小于结束日期。
但是它工作不正常。
请帮忙。
我等待你的回复。
谢谢
Start Date format is 23-Dec-2011(dd-MMM-yyyy)
End Date format is 28-Dec-2011(dd-MMM-yyyy)
I want to check validation in javascript(Client side) that is
Start date can not be less than end date.
But it not work properly.
Please help.
I am waiting for your response.
Thanks
推荐答案
function CompareFirstFieldDateGreaterThanSecondFieldDate(ctrlIDFirst, ctrlIDSecound, ctrlNameFirst,ctrlNameSecound)
{
var fieldDateFirst = document.getElementById(ctrlIDFirst).value ;
var fieldDateSecound = document.getElementById(ctrlIDSecound).value ;
if((fieldDateFirst == "")&&(fieldDateSecound == ""))
{
return true;
}
else
{
if(fieldDateFirst == "")
{
alert("Please enter " +ctrlNameFirst);
//document.getElementById(ctrlIDFirst).value = ""; -- Commented By Perumal on 23 July 2010
document.getElementById(ctrlIDFirst).focus();
return false;
}
else if(fieldDateSecound == "")
{
alert("Please enter " +ctrlNameSecound);
// document.getElementById(ctrlIDSecound).value = ""; -- Commented By Perumal on 23 July 2010
document.getElementById(ctrlIDSecound).focus();
return false;
}
else
{
fieldDateFirst = fieldDateFirst.split("/");
var Date1 = new Date();
Date1.setFullYear(fieldDateFirst[2],fieldDateFirst[1]-1,fieldDateFirst[0]);
fieldDateSecound = fieldDateSecound.split("/");
var Date2 = new Date();
Date2.setFullYear(fieldDateSecound[2],fieldDateSecound[1]-1,fieldDateSecound[0]);
if (Date1 > Date2)
{
return true;
}
else
{
//alert(" Please Enter "+ ctrlNameFirst + " and " + ctrlNameSecound +".");
alert(ctrlNameFirst + " should be greater than " + ctrlNameSecound + ".");
// document.getElementById(ctrlIDFirst).value = ""; -- Commented By Perumal on 23 July 2010
document.getElementById(ctrlIDFirst).focus();
return false;
}
}
}
}
这很好用
This works fine
Just使用Javascript的Date对象来定义日期,然后简单地使用日期对象比较。
Javascript日期对象:http://www.w3schools.com/js/js_obj_date.asp [ ^ ]
样本:
Just use the Date object of Javascript to define the dates and then simply use the date object comparison.
Javascript Date object: http://www.w3schools.com/js/js_obj_date.asp[^]
Sample:
var myDate=new Date();
myDate.setFullYear(2010,0,14);
var today = new Date();
if (myDate>today)
{
alert("Today is before 14th January 2010");
}
else
{
alert("Today is after 14th January 2010");
}
你可以尝试这段代码
http://devssolution.wordpress.com/2011/06/28/start-date-and-end -date-validation-in-asp-net / [ ^ ]
you can try this code
http://devssolution.wordpress.com/2011/06/28/start-date-and-end-date-validation-in-asp-net/[^]
这篇关于开始日期和结束日期验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文