How to do date validation from EditorFor using jquery? wanted to check if the date entered by the user, and if the date he entered is greater than the current date, display a message to him stating the error
<div class="form-group">
@Html.LabelFor(model => model.DataNascimento, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.DataNascimento, new { htmlAttributes = new { @class = "form-control data" } })
@Html.ValidationMessageFor(model => model.DataNascimento, "", new { @class = "text-danger" })
</div>
</div>
I thought of doing a jquery that takes the typed content and checks the date. Can anyone help me?
With the help of @LucasDeFreitasKauer this script was done:
$("#DataDeNascimento").on("blur", validarDataDeNascimento);
var validarDataDeNascimento = function () {
var dataDeNascimentoStr = $("#DataDeNascimento").val();
var dataDeNascimento = new Date(dataDeNascimentoStr);
var agora = new Date();
if (dataDeNascimento > agora) {
alert("A data de nascimento não pode ser uma data futura.");
}
};
and was added to the EditorFor the ID id="BirthDate" but when it loses focus it does not display any alerts.