Date problem with globalize.js and jquery.validation.globalize.js. How to solve?


I'm having problems with the date fields of my application.

Followingthisresponse,whichIfoundtobethemostappropriate, Error message in datetime field , I set up my ASP.NET MVC application as follows:

  • I installed the jquery-globalize package via Install-Package jquery-globalize ;
  • I installed the package jquery.validation.globalize via Install-Package jquery.validation.globalize ;

  • IaddedtheWeb.configtabtothelanguage..

    <system.web>...<globalizationuiCulture="pt-BR" culture="pt-BR" enableClientBasedCulture="true"
            requestEncoding="UTF-8" responseEncoding="UTF-8" fileEncoding="UTF-8" />

    Although the answer indicates to add in <configuration> , the application reported error in the Web.config file and then I got in the <system.web> tag.

  • I set up my BundleConfig.cs :

  • Iorganizedmyscriptsaccordingtotheresponseexample,thusleaving:

    The script with Globalize.culture("pt-BR"); at the end of the image was one last attempt I made.  Jquery.validation.js I load the other views, when I need them, via @section footerSection{ Scripts.Render("~/js/jqueryval"); } .  So my script output looks like this, in debug mode:

  • SoherearethepropertiesofmyViewModelusedtorenderthefields:

    [DataType(DataType.Date)][DisplayName("Data Vencto.")]
    [Required(ErrorMessage = "Informe o campo {0}")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? txtDataVencimento { get; set; }
    [DisplayName("Data Pagto.")]
    [Required(ErrorMessage = "Informe o campo {0}")]
    [DisplayFormat(ApplyFormatInEditMode = true, DataFormatString = "{0:dd/MM/yyyy}")]
    public DateTime? txtDataPagamento { get; set; }

    I already tested with DataFormatString = "{0:yyyy-MM-dd}" , but it also did not work.

  • Razor:

    <div class="form-group">
        @Html.LabelFor(model => model.txtDataVencimento, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.txtDataVencimento, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.txtDataVencimento, "", new { @class = "text-danger" })
    <div class="form-group">
        @Html.LabelFor(model => model.txtDataPagamento, new { @class = "control-label col-md-2" })
        <div class="col-md-10">
            @Html.EditorFor(model => model.txtDataPagamento, new { htmlAttributes = new { @class = "form-control" } })
            @Html.ValidationMessageFor(model => model.txtDataPagamento, "", new { @class = "text-danger" })
  • Then I ask you for help: What may still be missing or wrong?


    According to the response from Cigano Morrison , the order of my scripts should be different and I applied the Bundle accordingly.

    var bundle = new ScriptBundle("~/js/jqueryval") { Orderer = new AsIsBundleOrderer() };

    With this my scripts were in the order indicated:

    However,theerrorstillremains,butdisplayingamessageinEnglish:" Please enter a valid date. "




        bundles.Add(new ScriptBundle("~/js/jqueryval").Include(
        bundles.Add(new ScriptBundle("~/js/bootstrap").Include(
        bundles.Add(new StyleBundle("~/Static/css/styles").Include(

    And with the default sort order made by BundleConfig, my scripts were in that order:


    asked by anonymous 28.04.2015 / 15:48

    1 answer


    This is not a simple problem (it actually consumed me the effort of one night to find out). The problem is the order of the js that needs to be respected:

  • jquery.validate.js ;
  • jquery.validate.unobstrusive.js ;
  • globalize/globalize.js ;
  • jquery.validate.globalize.js .
  • Except the default implementation of ScriptBundle reorders even with the specified order correctly in Bundle .

    The way to implement Bundle is in order:

    public class AsIsBundleOrderer : IBundleOrderer
        public IEnumerable<BundleFile> OrderFiles(BundleContext context, IEnumerable<BundleFile> files)
            return files;

    And then:

            var bundle = new ScriptBundle("~/bundles/jqueryval") { Orderer = new AsIsBundleOrderer() };


    In the case of numbers, you need to work a bit on the numeric validator, which for some reason is not accepting a comma:

    // Esta parte pode ser colocada em um script da aplicação e juntado ao Bundle
    jQuery.validator.addMethod("mynumber", function (value, element) {
        return this.optional(element) || /^(\d+|\d+,\d{1,2})$/.test(value);
    }, "O campo " + element + " deve ser um número.");
    // Esta parte é para cada form
        rules: {
            field: {
                required: true,
                mynumber: true

    Or as the questioner suggested, use the example of Cleyton Ferrari , which is more or less the same thing. / p>     

    28.04.2015 / 16:12