Validate dates with Jquery validate for dd/mm/yyyy

Asked

Viewed 1,837 times

1

I want to validate the dates with Jquery Validate, but error, I researched a lot more the solutions found did not solve my problem.

my JS:

// incluir todos elementos do form na pagina
$("form").each(function () {
    // inicializa o plugin no each form
    $(this).validate({
        rules: {
            dataentrada: {
                dateBR: true
            },
            datavenc: {
                dateBR: true
            },
            dataproc: {
                dateBR: true

            }
        }
    });
}
);

//Valida Data PT-BR
jQuery.validator.addMethod("dateBR", function (value, element) {
    //contando chars
    if (value.length != 10)
        return this.optional(element) || false;
    // verificando data
    var data = value;
    var dia = data.substr(0, 2);
    var barra1 = data.substr(2, 1);
    var mes = data.substr(3, 2);
    var barra2 = data.substr(5, 1);
    var ano = data.substr(6, 4);
    if (data.length != 10 || barra1 != "/" || barra2 != "/" || isNaN(dia) || isNaN(mes) || isNaN(ano) || dia > 31 || mes > 12) {
        return this.optional(element) || false;
    }
    if ((mes == 4 || mes == 6 || mes == 9 || mes == 11) && dia == 31) {
        return this.optional(element) || false;
    }
    if (mes == 2 && (dia > 29 || (dia == 29 && ano % 4 !== 0))) {
        return this.optional(element) || false;
    }
    if (ano < 1900) {
        return this.optional(element) || false;
    }
    return this.optional(element) || true;
}, "Informe uma data válida"); /* Mensagem padrão */

When in browser I take the field focus, it shows invalid date message.

inserir a descrição da imagem aqui

1 answer

2

  1. Create a file named methods_pt.js with the content below.
  2. Add the file to the location where you call jquery (after)

See another way to do in: https://github.com/aspnet/Docs/issues/4076

jQuery.extend(jQuery.validator.methods, {
    date: function (value, element) {
        return this.optional(element) || /^\d\d?\/\d\d?\/\d\d\d?\d?$/.test(value);
    },
    number: function (value, element) {
        return this.optional(element) || /^-?(?:\d+|\d{1,3}(?:\.\d{3})+)(?:,\d+)?$/.test(value);
    }
});
  • I removed the above function and put this data. did not work, still displaying the same message with the correct date. See the sequence of my js at the head of the page: http://imgur.com/giNUPLll.png

  • https://jsfiddle.net/tjd5s0ba/3/ I put an example here, it doesn’t work.

  • 1

    Analyzing your profile, I saw that you use C#. In this case it would be better to use jquery validate unobtrusive. I use to perform this type of validation, since you can decorate a Viewmodel with data annotations and automate this process.

  • more in this case, I am using PHP.

  • 1

    I understood, from what I read tbm it is possible to use it in other languages tbm.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.