I'm programming in ASP.NET MVC and I have a registration form. I need to verify that the date of birth is valid, that is, if it is shorter than today's date. I have the script but I do not know how to call it inside the form
below, could anyone help me?
<div class="form-group">
<label><span class="req"></span> Data de Nascimento</label>
@Html.EditorFor(model => model.DataNascimento, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.DataNascimento, "", new { @class = "text-danger" })
</div>
I got this script from another response to implement:
function verificaData()
{
var dt = new Date();
var dia = dt.getDay();
var mes = dt.getMonth();
var ano = dt.getFullYear();
if(dia > 0 && dia < 10) dia = "0" + dia;
if(mes > 0 && mes < 10) mes = "0" + mes;
var dataAtual = dia + "/" + mes + "/" + ano;
var vDia = document.model.DataNascimento.value.substr(0,2);
var vMes = document.model.DataNascimento.value.substr(3, 2);
var vAno = document.model.DataNascimento.value.substr(6, 5);
if(vDia > dia ||
vMes > mes ||
vAno > ano)
{
alert("Data Inválida");
document.model.DataNascimento.value = dataAtual;
document.model.DataNascimento.focus();
}
}