I’ve Date field on my MVC UI named “startDate”, the user selects date using jquery date picker. As i wanted to validate that selected date should not be 2 months past and 2 months future.
I’ve wrote the below code for validating the date.
public sealed class DateAttribute : DataTypeAttribute
{
/// <summary>
/// Initializes a new instance of the <see cref="EmailAddressAttribute"/> class.
/// </summary>
public DateAttribute() : base(DataType.Date)
{
}
/// <summary>
/// Checks that the value of the data field is valid.
/// </summary>
/// <param name="value">The data field value to validate.</param>
/// <returns>
/// true always.
/// </returns>
public override bool IsValid(object value)
{
DateTime inputDate = Convert.ToDateTime(value, CultureInfo.CurrentCulture);
if (inputDate.Date >= DateTime.Now.Date.AddMonths(-2) && inputDate.Date <= DateTime.Now.Date.AddMonths(2))
return true;
return false;
}
}
But the issue is, it goes to server for validating the date field. how can i achive same with client validation.
Thanks,
-Naren
I factor the year up by 100, thereby avoiding cross year comparison
Then on your SPAN id=”x” onBlur=”IsValid(this.value)”>2001-01-01
Mike