定制不显眼的日期验证的日期 [英] custom unobtrusive date validators for dates

查看:183
本文介绍了定制不显眼的日期验证的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也许这只是我心中的工作方式,但我有一个非常困难的时间了解你应该怎么做定制不显眼的验证。 C#的部分是很容易的,但jQueryUI的适配器是在那里我迷路了。

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.

我一直在努力使需要的日期是在过去一定时间的验证。我用这个年龄验证,以确保有人输入了一个18年过去的某个日期。

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.

我终于决定只是使它远程验证,验证使用相同的code客户端和服务器端的方式。不过,我很想在jQuery来使这项工作。

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.

推荐答案

您可以找到大量的信息中的有关不显眼的审定与asp.net的MVC,包括创建自定义的验证布拉德·威尔逊的博客文章

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

根据下面的HTML(应该是文本框的辅助输出)

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"/>

您可以添加以下JavaScript得到的东西在客户端验证:

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;
    };

对于年龄计算器积分是由于<一个href=\"http://stackoverflow.com/questions/658522/age-from-date-of-birth-using-jquery/1197028#1197028\">Dave

一件事这里缺少的是全球化的,但我想这是不可能的范围之内。顺便说一句非常容易使用,以实现的jQuery插件全球化时代

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

希望这有助于

这篇关于定制不显眼的日期验证的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆