HTML5 datepicker onchange event

Asked

Viewed 54 times

-1

Can anyone tell me how to use datepicker onchange event to do a check on selected date?

in my cshtml is like this:

@Html.EditorFor(model => model.Dat_emissao, "{0:dd/MM/yyyy}", new { htmlAttributes = new { @class = "form-control", type = "date", @onchange = "checkDate2(this.value)" } })

and that would be my job:

var checkDate2 = function (obj) {
    if (obj._selectedDate > new Date()) {
        alert("Você não pode selecionar uma data maior que a data de hoje!");
        obj._selectedDate = new Date();
        // seleciona a data atual novamente
        obj._textbox.set_Value(obj._selectedDate.format(obj._format))
    }
}
  • not only you make the bind of the method checkDate2 after loading the page?

  • And note that in the statement @onchange = "checkDate2(this.value) you pass the value, while your method waits for the object

  • I made use of another function almost the same way and it worked, only this one with the date is not going.

  • Example that works: Html.Dropdownlistfor(model => model.Tip_doc, new Selectlist(listdoc, "Text", "Value"), "Select", new { class = "form-control", onchange = "validateDropDown(this.value)" })" var validateDropDown = Function (obj) { if (obj == "C") { Alert('Select a Database'); } Else { Return true; } }

  • You just tried what I said? @onchange = "checkDate2(this)? presents how the component in html was rendered and tests with javascript by the console

  • I arrived yes Leandro and was not, so I ended up doing otherwise, and I made the call on onclick="checkDate2()" that worked and now I’m struggling to buy two date on javascript rsrsr

  • I was able to solve the date question this way: var hoje = new Date(); var emissao = Document.getElementById("Emissao"). value; var emissaolMilisseconds = new Date(issuance). getTime() + 10000000; var hojeMilisseconds = new Date(today). getTime(); if (issuelMilisseconds > hojeMilisseconds) { mgs += "ISSUANCE DATE cannot be higher than today’s date! n"; errors = errors + 1; }

Show 2 more comments

1 answer

0

I ended up doing it this way:

        function VerificaDadosTitulo() {
            var erros = 0;
            var mgs = "";

            var hoje = new Date();
            var emissao = document.getElementById("Emissao").value;
            var vencimento = document.getElementById("Vencimento").value;
            var tipoDoc = document.getElementById("TipoDoc").value;
            var banco = document.getElementById("Banco").value;

            var emissaolMilissegundos = new Date(emissao).getTime() + 10000000;
            var vencimentoMilissegundos = new Date(vencimento).getTime();
            var hojeMilissegundos = new Date(hoje).getTime();

            if ((tipoDoc == "C") && (banco == "")) {
                mgs += "O tipo de documento Cheque, exige que seleciona o banco!\n";
                erros = erros + 1;
            }

            if (tipoDoc == "") {
                mgs += "O tipo de documento é obrigatório!\n";
                erros = erros + 1;
            }

            if (emissaolMilissegundos > hojeMilissegundos) {
                mgs += "Data de EMISSÃO não pode ser maior que a data de hoje!\n";
                erros = erros + 1;
            }
            if (vencimentoMilissegundos < hojeMilissegundos) {
                mgs += "Data de VENCIMENTO não pode ser menor ou igual a data de hoje!\n";
                erros = erros + 1;
            }

            if (erros > 0) {
                alert(mgs);
                event.preventDefault();
            }
        }

<input type="submit" class="btn btn-primary pull-right" value="Incluir" onclick="VerificaDadosTitulo()">

That way it worked!

Browser other questions tagged

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