MVc 5 - 使用 unobtrusiv js 验证德国日期 - 一种简单的方法 [英] MVc 5 - validation german date with unobtrusiv js - a simple approach

查看:12
本文介绍了MVc 5 - 使用 unobtrusiv js 验证德国日期 - 一种简单的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题:如何对在 MVC 中运行的德国日期进行不显眼的验证?

The question: How get the unobtrusiv validation of a german date running in MVC?

因为我找不到使用 globalize 1.x 和 MVC 5 来验证德国日期的运行示例,所以我需要两天才能让它运行.

Because I can't find a running example of using globalize 1.x with MVC 5 to validate a german date I needed two days to get it running.

问题在于 js 文件的顺序、获取 cldr 数据并将其以可重用的方式组合在一起.

The problems are the order of the js-files, getting the cldr-data and putting it all together in an way it can be reused.

在答案中,我将展示我当前的解决方案.

In the answer I will show my current solution.

推荐答案

在这个 zip 文件中 (https://www.dropbox.com/sh/75dx6alck7itwia/AABFkcgOQVc1bUXFE_jYfR_da?dl=0) 找到您需要的所有文件.

In this zip-file (https://www.dropbox.com/sh/75dx6alck7itwia/AABFkcgOQVc1bUXFE_jYfR_da?dl=0) you find all files you need.

包括

  • 一个简短的 todo.txt(de 和 en)
  • 子目录中的 cldr-data (jsons)
  • 一个自定义的 HTML-Helper-class 将所需的 HTML/js-Scripts 写入视图.

看来,助手的渲染并不总是有效.因此,如果有问题,请将代码复制到每个(编辑/新建)视图中.

It seems, that rendering by the helper not allways works. So if there are problems with that, copy the code to every (edit / new) view.

<script src="~/Scripts/jquery.validate.js"></script>
<script src="~/Scripts/jquery.validate.unobtrusive.js"></script>

<script src="~/Scripts/cldr.js"></script>
<script src="~/Scripts/cldr/event.js"></script>
<script src="~/Scripts/cldr/supplemental.js"></script>
<script src="~/Scripts/cldr/unresolved.js"></script>

<script src="~/Scripts/globalize.js"></script>
<script src="~/Scripts/globalize/currency.js"></script>
<script src="~/Scripts/globalize/number.js"></script>
<script src="~/Scripts/globalize/date.js"></script>
<script src="~/Scripts/globalize/plural.js"></script>
<script src="~/Scripts/globalize/relative-time.js"></script>
<script src="~/Scripts/globalize/unit.js"></script>
<script src="~/Scripts/jquery.validate.globalize.js"></script>        

<script>
    $(document).ready(function () {
        // Use $.getJSON instead of $.get if your server is not configured to return the
        // right MIME type for .json files.
        $.when(
            $.get("/Scripts/cldr/main/de/ca-gregorian.json"),
            $.get("/Scripts/cldr/main/de/numbers.json"),
            $.get("/Scripts/cldr/supplemental/likelySubtags.json"),
            $.get("/Scripts/cldr/supplemental/timeData.json"),
            $.get("/Scripts/cldr/supplemental/weekData.json")
        ).then(function () {
            // Normalize $.get results, we only need the JSON, not the request statuses.
            return [].slice.apply(arguments, [0]).map(function (result) {
                return result[0];
            });
        }).then(Globalize.load)
    .then(function () {
        Globalize.locale("de-DE");
    });
    });
</script>

希望能帮到你.

此解决方案基于 MVC 5 - 无法运行全球化.如果要使用捆绑包,请参阅 MVC 5,全球化,验证德国日期:如何捆绑 js 脚本?

This solution based on the answer to MVC 5 - can not get globalisation running. If you want to use a bündle, see MVC 5, globalize, validate german date: How to bundle the js-scripts?

这篇关于MVc 5 - 使用 unobtrusiv js 验证德国日期 - 一种简单的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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