在EN-GB日期不显眼的审定 [英] Unobtrusive Validation on en-GB Dates

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

问题描述

我使用的有型日期的输入asp.net MVC4设计数据输入表单。

I am designing a data input form using asp.net MVC4 which has an input of type date.

在使用Chrome和jQueryUI的日期选择我仍然得到一个错误(该场news_date必须是最新的。),选择一个正确的日期格式后,使用日期选择器不显眼的jQuery库(即14/02/2013)并提交的形式。

Using the unobtrusive jQuery library in chrome and jQueryUI datepicker I was still getting an error (The field news_date must be a date.), after selecting a correct date format, using the datepicker (i.e. 14/02/2013) and submitting the form.

搜索建议我使用了jQuery .globilization库。所以我添加到下面的脚本参考阅读文档后。

Some searching suggested I should use the jQuery.globilization library. So after reading the documentation I added a reference to the following script.

$(document).ready(function () {
    $.culture = Globalize.culture("en-GB");
    $.validator.methods.date = function (value, element) {
        return this.optional(element) || Globalize.parseDate(value, "dd/MM/yyyy", "en-GB");
    }
});

不过我仍然得到错误。

However I am still getting the error

如果我调试上述code铬我可以看到,价值的值)为2014年2月14日。

If I debug the the above code in chrome i can see that the value of "value) is "2014-02-14".

下面是一个被呈现的HTML。

Below is html which gets rendered.

<div class="editor-field">

<input class="text-box single-line datepicker hasDatepicker input-validation-error" data-val="true" data-val-date="The field news_date must be a date." id="news_date" name="news_date" type="date" value="">
<script type="text/javascript">
    $(function () {
        $(".datepicker").datepicker({ dateFormat: "dd/MM/yyyy" });
    });
    </script>
            <span class="field-validation-error" data-valmsg-for="news_date" data-valmsg-replace="true"><span for="news_date" class="" style="">The field news_date must be a date.</span></span>
        </div>

更新:顺便说一句,如果我改变我的验证方法很难code。
即。

UPDATE: incidentally if i change my validator method to hardcode. i.e.

$.validator.methods.date = function (value, element) {
    return this.optional(element) || Globalize.parseDate("14/03/2014", "dd/MM/yyyy", "en-GB")
}

它的工作原理。因此,它肯定似乎是日期被存储张贴或提交验证这是问题之前页面上的格式。

It works. So it definately appears to be the format that the date is being stored on the page prior to POST or submit validation which is the issue.

更新
              选择我办 $('#news_date')之日起。VAL()在Chrome控制台,我得到2014年2月14日,我可以调试我脚本,并看到价值传递它验证方法,正是这种价值,因而它不匹配因而英国的日期格式返回一个空值,而不是这将通过验证的值。所以我是用日期选择器是如何选择存储后的日期问题?

UPDATE after selecting the date if i run $('#news_date').val() in the chrome console, i get "2014-02-14" and i can debug my script and see the value being passed it the validator method it is this value and thus it doesnt match the british date format thus returning a null, rather than a value which would pass validation. So is my problem with how the datepicker is storing the date after selection?

有没有人找到了一种解决方法在Chrome?

Has anyone found a workaround for this in Chrome?

推荐答案

这个解决办法似乎处理Chrome和IE等。在铬我不能键入一个不正确的日期,这样的日期选择器会给我一个有效的日期,当它通过它的功能验证这将是YYYY-MM-DD格式。即让我直接输入到输入或使用选择器,在一个无效的日期输入即2013年2月30日(无效的EN-GB文化)它正确无效它和prevents取得进一步进展。如果没有更好的建议,我认为我去与此为最佳可行的答案

This workaround appears to handle chrome and ie etc. In chrome I cannot type in an incorrect date so the date picker will give me a valid date and when it passes it to the validator function it will be in the yyyy-mm-dd format. i.e lets me type straight into the input or use the picker, typing in an invalid date i.e. 2/30/2013 (invalid in en-GB culture) it correctly invalidates it and prevents further progress. If no better suggestions I think ill go with this as the best workable answer

$(document).ready(function () {
$.culture = Globalize.culture("en-GB");
$.validator.methods.date = function (value, element) {
    //This is not ideal but Chrome passes dates through in ISO1901 format regardless of locale 
    //and despite displaying in the specified format.

    return this.optional(element)
        || Globalize.parseDate(value, "dd/mm/yyyy", "en-GB")
        || Globalize.parseDate(value, "yyyy-mm-dd");
}});

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

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