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.
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
– JB_
https://jsfiddle.net/tjd5s0ba/3/ I put an example here, it doesn’t work.
– JB_
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.
– Braytiner
more in this case, I am using PHP.
– JB_
I understood, from what I read tbm it is possible to use it in other languages tbm.
– Braytiner