开发者

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 <= 开发者_JAVA技巧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


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜