Unobtrusive Validation on en-GB Dates

后端 未结 3 840
渐次进展
渐次进展 2021-01-05 15:15

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

Using the unobtrusive jQuery library in chrome and jQueryUI datepicker I was sti

相关标签:
3条回答
  • 2021-01-05 15:33
    $(function () {
            $(".datepicker").datepicker({ dateFormat: 'dd/mm/yy' });
     });
    
    0 讨论(0)
  • 2021-01-05 15:37

    Can i suggest you another work around, Put this in your web.config to avoid issues with differenet culture settings.

    <globalization requestEncoding="utf-8" responseEncoding="utf-8" culture="en-GB" uiCulture="en-GB"/>
    

    And in Jquery datepicker this would be enough,

     $(function () {
            $(".datepicker").datepicker({ dateFormat: "dd/MM/yyyy" });
     });
    
    0 讨论(0)
  • 2021-01-05 15:56

    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");
    }});
    
    0 讨论(0)
提交回复
热议问题