Reputation: 605
I have created a Custom Validation Attribute:
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;
}
}
The above code need post back to server, how can I get this to work on client side too with jquery?
Thanks, -Naren
Upvotes: 2
Views: 1351
Reputation: 1459
Stuart Leeks has a blog post about building a date range validator, including client side, with jquery datepicker.
It might be more than you need, but it definitely will fulfill what you're looking for.
Upvotes: 4
Reputation: 8667
You can't make this code work client-side automagically. You will have to write the similar validation code in JavaScript, and configure jquery.validation to apply this validation method.
Upvotes: 2