JavaScript来验证在asp.net中开始日期和结束日期 [英] Javascript to validate start date and end date in asp.net
问题描述
我写了应该检查开始日期小于结束日期的JS。如果没有,警报应该抛出
I have written a JS that should check that start date is less than end date. If not, alert should be thrown
的JS写为;
function DateValidation(startDate, EndDate) {
debugger;
var stdate = startDate;
var enddate = EndDate;
if (stdate!= '' && enddate!='') {
if (stdate > enddate) {
alert('Start date cannot be greater than end date');
return false;
}
else {
return true;
}
}
}
当我点击一个按钮显示报告。
This JS gets fired when i am clicking a button as "Show Report".
问题是我面临的
-
JS不正确验证的日期。我在想什么?我传递日期从文本框
JS doesn't validate the date correctly. What am i missing? i am passing date from the textbox
在第一次点击按钮时,JS不解雇了。它激发点击按钮第二遍的时候
The JS doesn't fired up when clicking button for the first time. it fires when clicking the button second time
另外,我已经注册了JS如下;
Plus, i have registered the JS as below;
btnShowReport.Attributes.Add("onclick", "return DateValidation('" + txtStartDate.Text + "', '" + txtEndDate.Text + "');");
是上面code正确吗?什么是注册JS正确的位置?
Is the above code correct? What is the correct place to register the JS?
请指导..谢谢!
推荐答案
您需要将字符串值解析为日期
You need to parse the string values to dates
if (startDate!= '' && EndDate!='') {
var stdate = Date.parse(startDate);
var enddate = Date.parse(EndDate);
if (stdate > enddate) {
alert('Start date cannot be greater than end date');
return false;
}
else
{
return true;
}
}
如果没有进一步的code这是很难说为什么你的按钮仅触发在第二次点击的事件。是您的按钮禁用下手?
Without further code it's hard to tell why your button only fires the event on the second click. Is your button disabled to start with?
这篇关于JavaScript来验证在asp.net中开始日期和结束日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!