2

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

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

Comments

2

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.

2 Comments

Can you please help me, how can i write similarr code in javascript and what are the steps to be taken.
This doesn't sound easy to me. You'll have to parse the date into JavaScript Date object, and the parsing will vary with different cultures. If I were you, I would think of some other way of date handling. For example, jqueryui.com/demos/datepicker

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.