Narendra V
Narendra V

Reputation: 605

Jquery client validation for custom attribute

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

Answers (2)

MHollis
MHollis

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.

ASP.NET MVC 3: Integrating with the jQuery UI date picker and adding a jQuery validate date range validator

Upvotes: 4

Zruty
Zruty

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

Related Questions