我需要我的结束日期总是晚于开始日期,我尝试使用CompareValidator进行验证。
代码如下:
我有一个文本框开始日期
<asp:TextBox ID="TxtStartDate"
runat="server" />
<asp:CalendarExtender Enabled="True"
ID="TxtStartDate_CalendarExtender"
TargetControlID="TxtStartDate"
runat="server" />另一个TextBox结束日期。
<asp:TextBox ID="TxtEndDate"
runat="server" />
<asp:CalendarExtender Enabled="True"
ID="TxtEndDate_CalendarExtender"
TargetControlID="TxtEndDate"
runat="server" />
<asp:CompareValidator ControlToCompare="TxtStartDate"
ControlToValidate="TxtEndDate"
Display="Dynamic"
ErrorMessage="CompareValidator"
ID="CompareValidator1"
Operator="GreaterThan"
Type="Date"
runat="server" />但是比较字段验证器失效了。
例如,当开始日期是2/04/2012,结束日期是10/04/2012时,它会触发。
发布于 2012-05-21 20:15:12
简单地说,您可以像这样尝试
<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate"
ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual"
ErrorMessage="*Invalid Data" runat="server"></asp:CompareValidator>发布于 2012-10-12 17:47:42
这个是正确的..它解决了我的问题。
<asp:CompareValidator ID="cmpVal1" ControlToCompare="txtStartDate" ControlToValidate="txtEndDate" Type="Date" Operator="GreaterThanEqual" ErrorMessage="ToDate should be greater than FromDate" runat="server"></asp:CompareValidator>别忘了写:
cmpVal1.Validate() 在发生比较的事件上。
发布于 2018-02-06 02:49:45
使用JAVASCRIPT验证开始日期(自)和结束日期(至)
它关心开始日期和结束日期都应该小于今天日期(当前日期)。起始日期应小于截止日期。
文本框
<asp:TextBox ID="txtFromDate" runat="server" onChange="javascript: txtFromDateChanged(this)"></asp:TextBox>
<asp:TextBox ID="txtToDate" runat="server" onChange="javascript: txtToDateChanged(this);" ></asp:TextBox>java脚本(将脚本块放入Head部分)
<script type="text/javascript">
function txtToDateChanged(sender, args) {
var date = new Date();
var startDate = Date.parse(document.getElementById('<%= txtFromDate.ClientID %>').value);
var endDate = Date.parse(document.getElementById('<%= txtToDate.ClientID %>').value);
var timeDiff = endDate - startDate;
var daysDiff = Math.floor(timeDiff / (1000 * 60 * 60 * 24));
if (date > endDate) {
document.getElementById('<%= txtToDate.ClientID %>').value = "";
alert('*Select date greater than Today.');
}
if (daysDiff < 0) {
document.getElementById('<%= txtToDate.ClientID %>').value = "";
alert('*FromDate should be less than Todate');
}
}
function txtFromDateChanged(sender, args) {
var date = new Date();
var startDate = Date.parse(document.getElementById('<%= txtFromDate.ClientID %>').value);
if (date > startDate) {
document.getElementById('<%= txtFromDate.ClientID %>').value="";
alert('*Select date greater than Today.');
}
}
</script>https://stackoverflow.com/questions/10177224
复制相似问题