Last active
October 7, 2018 21:29
-
-
Save leandrocustodio/5467412 to your computer and use it in GitHub Desktop.
Extensão Jquery Validate.
Validar data no formato dd/mm/aaaa
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/*Aplicando a validação*/ | |
$(".boxFormCadastro form").validate({ | |
errorLabelContainer: "#messageBox", | |
rules: { | |
dataNascimento: { | |
required: true, | |
dateBR: true | |
} | |
}, | |
messages: { | |
dataNascimento: "Campo obrigatório.", | |
} | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Este método pode ser adicionado dentro do $('ducument').ready(); | |
*/ | |
$.validator.addMethod("dateBR", function(value, element) { | |
if(value.length!=10) return 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 false; | |
if((mes==4||mes==6||mes==9||mes==11) && dia==31)return false; | |
if(mes==2 && (dia>29||(dia==29 && ano%4!=0)))return false; | |
if(ano < 1900)return false; | |
return true; | |
}, "Informe uma data válida"); // Mensagem padrão |
Como que eu uso este plugin ?
Troca todas as suas condições no validateExtBR, por uma RegExp
if(value.length!=10) return false;
return value.match(/^(0[1-9]|[12][0-9]|3[01])[- \/.](0[1-9]|1[012])[- \/.](19|20)\d\d$/);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Deveria ter
if(value == "") return true;
, para que a validação de obrigatoriedade seja feita apenas pelorequired: true