hariaspind
asked on
Comparing two dates.Ensure Enddate is greater then start date.
Dose any one know how to write a custom validation with data annotation using ValidationAttribute .
ASKER
I know this .I am working with Asp.net MVC2.0 .So iam looking to create a custom validation class using ValidationAttribute .
Will this work? Might require some modification, untested
public class DateEqualityAttribute : ValidationAttribute
{
public DateEqualityAttribute(Date Time startDate, DateTime endDate)
: base()
{
StartDate = startDate;
EndDate = endDate;
}
public override bool IsValid(object value)
{
if (value != null)
{
return StartDate < EndDate;
}
return true;
}
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
public class DateEqualityAttribute : ValidationAttribute
{
public DateEqualityAttribute(Date
: base()
{
StartDate = startDate;
EndDate = endDate;
}
public override bool IsValid(object value)
{
if (value != null)
{
return StartDate < EndDate;
}
return true;
}
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
ASKER
I had the similar thought.Would you please let me know how to use this DateEqualityAttribute.Beca use my ViewModel has two properties as below
public class FundTypeEditViewModel : ViewModelBase
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
so how to call the DateEqualityAttribute in my ViewModel
public class FundTypeEditViewModel : ViewModelBase
{
public DateTime StartDate { get; set; }
public DateTime EndDate { get; set; }
}
so how to call the DateEqualityAttribute in my ViewModel
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks
<asp:TextBox ID="TextBox1" runat="server"></asp:TextB
<asp:CompareValidator ID="CompareValidator1" runat="server" ControlToValidate="TextBox
ErrorMessage="CompareValid
and set CompareValidator1.ValueToC
CompareValidator1.ValueToC