Just wandering, how can I validate the date is not the future date in .net c#.
Example:
I have applied a validation there for required field validation. But somehow I have no idea how to apply the validation to check the start date to make sure it's not the future date (date not greater then the current date)?
<tr>
<td align="right">Start Date:</td>
<td><asp:TextBox runat="server" ID="activeDate" size="8"/>(YYYY-MM-DD)
<asp:RequiredFieldValidator ID="reqvactiveDate" runat="server"
ControlToValidate="activeDate" Display="Dynamic" EnableClientScript="true"
ErrorMessage="required" />
</td>
</tr>
than I wrote the following code to tried out the date validation. The date validation doesn't seem working for me:(
<tr>
<td align="right">Start Date:</td>
<td><asp:TextBox runat="server" ID="activeDate" size="8"/>(YYYY-MM-DD)
<asp:RequiredFieldValidator ID="reqvactiveDate" runat="server"
ControlToValidate="activeDate" Display="Dynamic" EnableClientScript="true"
ErrorMessage="required" />
<asp:CustomValidator runat="server"
ID="valDateRange"
ControlToValidate="activeDate"
onservervalidate="valDateRange_ServerValidate"
ErrorMessage="enter valid date" />
</td>
</tr>
code behind:
protected void valDateRange_ServerValidate(object source, ServerValidateEventArgs args)
{
DateTime minDate = DateTime.Parse("1000/12/28");
DateTime maxDate = DateTime.Parse("2011/05/26");
DateTime dt;
args.IsValid = (DateTime.TryParse(args.Value, out dt)
&& dt <= maxDate
&& dt >= minDate);
}
DateTime implements an IComparer interface. Check if its greater than DateTime.Now
There is no reason to parse it, just do:
if(datetime1>datetime2)
{
....
}
I think you're getting the error for this: DateTime.Parse("1000/12/28")
Instead, try DateTime.MinValue
. You can also optionally use compareValidators..
Also, any reason you are not just doing [your date] < DateTime.now?
Here is my code that does the validation of the date value not to be in the future on the client side :
<tr>
<td align="right">
Start Date:
</td>
<td>
<asp:TextBox runat="server" ID="txtActiveDate" size="8"/>(YYYY-MM-DD)
<!-- Validate if the entered date value is valid -->
<asp:CompareValidator ID="cvIsActiveDateValid" runat="server"
ControlToValidate="txtActiveDate"
ErrorMessage="Invalid Start Date"
Operator="DataTypeCheck"
Type="Date"></asp:CompareValidator>
<!-- Validate if the entered value is not future dated -->
<asp:CompareValidator ID="cvIsActiveDateNotInFuture" runat="server"
ControlToValidate="txtActiveDate"
ErrorMessage="Start Date cannot be a future date."
Operator="LessThanEqual"
Type="Date"></asp:CompareValidator>
</td>
</tr>
protected void Page_Load(object sender, EventArgs e)
{
SetTodaysDateToCompareValidators();
...
}
protected void SetTodaysDateToCompareValidators()
{
string defaultDateFormat = "YYYY-MM-DD";
string today = DateTime.Today.ToString(defaultDateFormat);
cvActiveDate.ValueToCompare = today;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.