简体   繁体   English

Jquery 客户端验证自定义属性

[英]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?上面的代码需要回传到服务器,我怎样才能让它在客户端也能用 jquery 工作?

Thanks, -Naren谢谢,-纳伦

Stuart Leeks has a blog post about building a date range validator, including client side, with jquery datepicker. Stuart Leeks 有一篇关于使用 jquery 日期选择器构建日期范围验证器(包括客户端)的博客文章。

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 ASP.NET MVC 3:与 jQuery UI 日期选择器集成并添加 jQuery 验证日期范围验证器

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.您必须在 JavaScript 中编写类似的验证代码,并配置 jquery.validation 以应用此验证方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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