自定义不引人注目的日期验证器

发布于 2024-12-01 12:54:33 字数 244 浏览 3 评论 0原文

也许这只是我的思维方式,但我很难理解你应该如何做定制的不引人注目的验证器。 C# 部分很简单,但 jqueryui 适配器是我迷失的地方。

我一直在尝试制作一个验证器,要求日期是过去的一定时间。我用它来进行年龄验证,以确保某人输入的日期是 18 年前的日期。

我最终决定将其设为远程验证器,这样验证在客户端和服务器端都使用相同的代码。尽管如此,我还是对 jquery 感兴趣,以完成这项工作。

我希望数据注释扩展具有日期函数。

Maybe it's just the way my mind works, but I have a very hard time understanding how you're supposed to do custom unobtrusive validators. The C# part is easy enough, but the jqueryui adapters are where i get lost.

I've been trying to make a validator that requires a date to be a certain amount of time in the past. I use this for age validation, to make sure someone has entered a date that is 18 years in the past.

I finally decided to just make it a remote validator, that way the validation uses the same code both client and server side. Still, i'd be interested in the jquery to make this work.

I wish the Data Annotation Extensions had date functions.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

掀纱窥君容 2024-12-08 12:54:34

您可以在 Brad Wilson 的博客文章 关于使用 ASP.NET MVC 进行不显眼的验证,包括创建自定义验证器。

基于以下 html(应该是 TextBox 帮助程序的输出)

    <input type="text" name="Age"
        data-val="true" 
        data-val-required="This field is required" 
        data-val-minage="You should be 18 years or older, go get your parents!" 
        data-val-minage-value="18" />
    <input type="submit"/>

您可以添加以下 javascript 以在客户端验证内容:

    // Extend date with age calculator
    Date.prototype.age = function (at) {
        var value = new Date(this.getTime());
        var age = at.getFullYear() - value.getFullYear();
        value = value.setFullYear(at.getFullYear());
        if (at < value) --age;
        return age;
    };

    // Add adapter for minimum age validator. Wrap in closure
    (function ($) {
        $.validator.unobtrusive.adapters.addSingleVal("minage", "value");
    } (jQuery));

    // Add client side minimum age validator
    $.validator.methods.minage = function (value, element, params) {

        // If no value is specified, don't validate
        if (value.length == 0) {
            return true;
        }

        var dob = new Date(Date.parse(value));

        if (dob.age(new Date()) < params || dob == 'Invalid Date') {
            return false;
        }

        return true;
    };

年龄计算器的积分来自 Dave

这里缺少的一件事是全球化,但我认为它已经结束了问题的范围。顺便说一句,使用 jquery Globalize 插件 很容易实现

希望这有帮助

You can find a lot of information in Brad Wilson's blog article about unobtrusive validation with asp.net mvc, including creating custom validators.

Based on the following html (should be the output of the TextBox helper)

    <input type="text" name="Age"
        data-val="true" 
        data-val-required="This field is required" 
        data-val-minage="You should be 18 years or older, go get your parents!" 
        data-val-minage-value="18" />
    <input type="submit"/>

You can add the following javascript to get things validated on the client side:

    // Extend date with age calculator
    Date.prototype.age = function (at) {
        var value = new Date(this.getTime());
        var age = at.getFullYear() - value.getFullYear();
        value = value.setFullYear(at.getFullYear());
        if (at < value) --age;
        return age;
    };

    // Add adapter for minimum age validator. Wrap in closure
    (function ($) {
        $.validator.unobtrusive.adapters.addSingleVal("minage", "value");
    } (jQuery));

    // Add client side minimum age validator
    $.validator.methods.minage = function (value, element, params) {

        // If no value is specified, don't validate
        if (value.length == 0) {
            return true;
        }

        var dob = new Date(Date.parse(value));

        if (dob.age(new Date()) < params || dob == 'Invalid Date') {
            return false;
        }

        return true;
    };

Credits for the age calculator are due to Dave

The one thing missing here is globalization, but I figured it was out of the question's scope. btw very easy to implement using the jquery Globalize plugin

Hope this helps

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文