出生日期、加入日期和离开日期的验证



我有三个用于BirthDateJoiningDate&LeavingDate

我试图确保的是:

  1. Joining dateLeaving date不应大于Birthdate
  2. CCD_ 7不应大于CCD_ 8和CCD_

当输入了错误的条目时,我想抛出一个错误。

我已经使用DatePicker从用户那里获取日期输入。

这是我正在使用的JS代码:-

$(function () {
$("[id$=mainContent_txtdateofbirth], [id$=mainContent_txtdoj], [id$=mainContent_txtdol]").datepicker({
textboxImageOnly: true,
textboxImage: 'images/calendar.png',
changeMonth: true,
changeYear: true,
dateFormat: "yy / mm / dd",
yearRange: "-40:+0",
maxDate: new Date(),
});
});

我正在使用的文本框代码:-

<asp:TextBox ID="txtdateofbirth" CssClass="form-control" runat="server" ValidationGroup="AddNew"></asp:TextBox>
<asp:TextBox ID="txtdoj" CssClass="form-control" runat="server" ValidationGroup="AddNew"></asp:TextBox>
<asp:TextBox ID="txtdol" CssClass="form-control" runat="server" ValidationGroup="AddNew"></asp:TextBox>

我从这里得到了解决方案,但它不是像我试图做的那样点击按钮。

这是我的相关代码:-

tr>
<td style="vertical-align: top;">
<label class="control-label" for="dob">Date of Birth</label></td>
<td>
<div class="control-group">
<div class="controls">
<asp:TextBox ID="txtdateofbirth" CssClass="form-control" autocomplete="off" runat="server" ValidationGroup="AddNew"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqdob" runat="server" CssClass="error-class" ControlToValidate="txtdateofbirth" ErrorMessage="Please select the date of birth" ValidationGroup="AddNew"></asp:RequiredFieldValidator>
</div>
</div>
</td>
</tr>
<tr>
<td style="vertical-align: top;">
<label class="control-label" for="subject">Date of Join</label></td>
<td>
<div class="control-group">
<div class="controls">
<asp:TextBox ID="txtdoj" Wrap="true" CssClass="form-control" autocomplete="off" runat="server" ValidationGroup="AddNew"></asp:TextBox>
<asp:RequiredFieldValidator ID="reqdoj" CssClass="error-class" runat="server" ControlToValidate="txtdoj" ErrorMessage="Please add the date of joining" ValidationGroup="AddNew"></asp:RequiredFieldValidator>
</div>
</div>
</td>
</tr>
<tr>
<td style="vertical-align: top;">
<label class="control-label" for="subject">Date of Leaving</label></td>
<td>
<div class="control-group">
<div class="controls">
<asp:TextBox ID="txtdol" CssClass="form-control" autocomplete="off" runat="server" ValidationGroup="AddNew"></asp:TextBox>
</div>
</div>
</td>
</tr><asp:Button ID="btnSubmit" runat="server" CssClass="btn btn-prm" Text="Submit" Width="75" CausesValidation="true" ValidationGroup="AddNew" OnClick="btnSubmit_Click" />

请建议我如何根据上面列出的标准验证这些日期。

EDIT 2从您发布的评论来看,将所有部分组合在一起似乎比我预期的要困难,因此以下是您需要做的事情,以获得您想要的结果:

  1. 在服务器端为您的按钮点击设置一个事件
  2. 在该函数中,将日期选择器的值分配给3个独立的变量
  3. 使用下面针对C#的示例代码,比较leavedatejoindatebirthdate,并确定leavedatejoindate是出现在birthdate之前还是之后
  4. 继续执行您的应用程序执行的其他操作

编辑:这是MSDN上另一篇关于如何连接按钮点击的文章。因此,如果你把所有的部分放在一起,你会在点击按钮时得到你想要的验证日期的结果。

比较日期,尤其是以你想要的方式比较,相对来说比较简单。

要在服务器端(在C#中)做到这一点,这里有一篇MSDN文章和示例:

using System;
public class Example
{
public static void Main()
{
DateTime date1 = new DateTime(2009, 8, 1, 0, 0, 0);
DateTime date2 = new DateTime(2009, 8, 1, 12, 0, 0);
int result = DateTime.Compare(date1, date2);
string relationship;
if (result < 0)
relationship = "is earlier than";
else if (result == 0)
relationship = "is the same time as";         
else
relationship = "is later than";
Console.WriteLine("{0} {1} {2}", date1, relationship, date2);
}
}
// The example displays the following output: 
//    8/1/2009 12:00:00 AM is earlier than 8/1/2009 12:00:00 PM

要在客户端(用Javascript)完成这项工作,这里有一篇W3Schools的文章和示例:

var today, someday, text; today = new Date(); someday = new Date(); someday.setFullYear(2100, 0, 14);
if (someday > today) {
text = "Today is before January 14, 2100."; } else {
text = "Today is after January 14, 2100."; } document.getElementById("demo").innerHTML = text;

我在这里做了一些研究,并编写了以下代码:-

$(function () {
$("[id$=mainContent_txtdateofbirth]").datepicker({
textboxImageOnly: true,
textboxImage: 'images/calendar.png',
changeMonth: true,
changeYear: true,
dateFormat: "dd / mm / yy",
yearRange: "-40:+0",
onSelect: function (selected) {
$("#mainContent_txtdoj").datepicker("option", "minDate", selected)
$("#mainContent_txtdol").datepicker("option", "minDate", selected)
}
});
$("[id$=mainContent_txtdoj]").datepicker({
textboxImageOnly: true,
textboxImage: 'images/calendar.png',
changeMonth: true,
changeYear: true,
dateFormat: "dd / mm / yy",
yearRange: "-40:+0",
onSelect: function (selected) {
$("#mainContent_txtdateofbirth").datepicker("option", "maxDate", selected)
$("#mainContent_txtdol").datepicker("option", "minDate", selected)
}
});
$("[id$=mainContent_txtdol]").datepicker({
textboxImageOnly: true,
textboxImage: 'images/calendar.png',
changeMonth: true,
changeYear: true,
dateFormat: "dd / mm / yy",
yearRange: "-40:+0",
onSelect: function (selected) {
$("#mainContent_txtdateofbirth").datepicker("option", "maxDate", selected)
$("#mainContent_txtdoj").datepicker("option", "maxDate", selected)
}
});
});

相反,但它有效。。!!!

最新更新