Javascript date validation using aaa-mm-dd PHP dates

Asked

Viewed 29 times

0

Fala galera...

Next, I need to implement a student’s "Suspend Contract" option. This suspension may only be applied within the term of the student’s contract. Assuming that the student has his contract valid from 01/01/2020 until 31/12/2022 and the suspension of this contract should ONLY be applied between these two dates.

I have the code below, but I don’t know where or what’s wrong.

<script language="javascript">
    function checarDatas()
    {
        var dataCI = (document.forms[0]["contratoIncio"].value).split("/");
        var dataCF = (document.forms[0]["contratoFim"].value).split("/");

        var dataSI = (document.forms[0]["suspensaoInicio"].value).split("/");
        var dataSF = (document.forms[0]["suspensaoFim"].value).split("/");

        var dataInformadaSI = new Date(dataSI[2], dataSI[1]-1, dataSI[0]);
        var dataInformadaSF = new Date(dataSF[2], dataSF[1]-1, dataSF[0]);      

        var dataInformadaCI = new Date(dataCI[2], dataCI[1]-1, dataCI[0]);
        var dataInformadaCF = new Date(dataCF[2], dataCF[1]-1, dataCF[0]);      


        if ( dataInformadaSI >= dataInformadaCI && dataInformadaSF <= dataInformadaCF )
        {
            alert("Datas OK!");
        }
        else 
        {
            alert("A data de suspensao escolhida, esta fora da vigencia do contrato.");
            document.getElementById('suspensaoInicio').value="";
            document.getElementById('suspensaoFim').value="";
        }
    }
</script>

My page is in PHP. I have a query that brings from BD the contract dates in the format "yyyy-mm-dd".

I hope I was clear.

Thank you who can help!

1 answer

0


I was able to make it run like I had to!

Compare the two codes and see what was wrong with the first one. The code below is running 100% as accurate.

<script language="javascript">
    function checarDatas()
    {
        var dataCI = document.getElementById('contratoInicio').value;
        var dataCF = document.getElementById('contratoFim').value;

        var dataSI = document.getElementById('suspensaoInicio').value;
        var dataSF = document.getElementById('suspensaoFim').value;

        if ( dataSI >= dataCI && dataSF <= dataCF )
        {
//          alert("Datas OK!");
        }
        else 
        {
            alert("O periodo de suspensao esta fora da vigência do contrato.");
            document.getElementById('suspensaoInicio').value="";
            document.getElementById('suspensaoFim').value="";
        }
    }
</script>

Browser other questions tagged

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