简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM