How to validate Date and Time to check together if they are larger than the current date and time

Asked

Viewed 25 times

1

Sirs.
I am trying to validate two fields (Date and Time) because they cannot be greater than the current date and time.
There is a validation on the date but I need to validate the date and time together pq if the current date is 17/07/2019 10:40 and the person typing 16/07/2019 23:00 has to give the message that the date and time of the discharge cannot be greater than the current date and time.

Home Dischargeformedit.aspx

<gens:GensLabel ID="labelAdmission" runat="server" Font-Bold="True"></gens:GensLabel>

<td>
    <span style="float: right;">
        <gens:GensMaskEditDate ID="maskEditDateDischarge" runat="server" Width="72px"></gens:GensMaskEditDate>
        <gens:GensRequiredValidator ID="requiredValidatorDateDischarge" runat="server" ControlToValidate="maskEditDateDischarge"
            ErrorMessage="Informe a data da alta">*</gens:GensRequiredValidator>
        <gens:GensCompareValidator ID="compareValidatorDateAdmission" runat="server" ControlToValidate="maskEditDateDischarge"
            Operator="GreaterThanEqual" Type="Date" ErrorMessage="A data da alta deve ser maior ou igual a data de admissão">*</gens:GensCompareValidator>
        <gens:GensCompareValidator ID="_compareValidatorDateAdmission" runat="server" ControlToValidate="maskEditDateDischarge"
            Operator="LessThanEqual" Type="Date" ErrorMessage="A data da alta deve ser menor ou igual a data de hoje.">*</gens:GensCompareValidator>
    </span>
    <span style="float: right; vertical-align: middle; padding-right: 2px;">
        <gens:GensLabel ID="GensLabel4" runat="server">Data da Alta:</gens:GensLabel>
    </span>
</td>

<td style="width: 170px;">
    <span style="float: right;">
        <gens:GensMaskEditTime ID="maskEditTimeDischarge" runat="server" Width="40px" DateFormat="HHMM"></gens:GensMaskEditTime>
        <gens:GensRequiredValidator ID="requiredValidatorTimeDischarge" runat="server" ControlToValidate="maskEditTimeDischarge"
            ErrorMessage="Informe a hora da alta">*</gens:GensRequiredValidator>
        <gens:GensCustomValidator ID="customValidatorTimeAdmission" runat="server" 
            ErrorMessage="Hora da alta não pode ser menor que hora de admissão!" ControlToValidate="TxBSisPreNatal" 
            ClientValidationFunction="ValidateTimeAdmission">*</gens:GensCustomValidator>   

        <gens:GensRequiredValidator ID="GensRequiredValidator3" runat="server" ControlToValidate="maskEditTimeDischarge"
            ErrorMessage="A Data e Hora da alta não podem ser maiores que a data e hora atual.">*</gens:GensRequiredValidator>                                    
    </span>
    <span style="float: right; vertical-align: middle; padding-right: 2px;">
        <gens:GensLabel ID="GensLabel10" runat="server">Hora da Liberação:</gens:GensLabel>                                    
    </span>
</td>

Home Dischargeformedit.aspx.Cs

using (Discharges discharges = new Discharges())
                    {
                        if (recordState == RecordState.Updating)
                        {
                            if (discharges.isDeath(admissionId))
                            {
                                dropDownListReasonDischarge.DataSource = discharges.GetDischargeReasonDeath(isSus);
                            }
                            else
                            {
                                dropDownListReasonDischarge.DataSource = discharges.GetDischargeReason(isSus);
                            }
                        }
                        else
                        {
                            dropDownListReasonDischarge.DataSource = discharges.GetDischargeReason(isSus);
                        }
                        dropDownListReasonDischarge.DataBind();

                        dropDownListAccidentIndication.DataSource = discharges.GetAccidentIndicationByGuideType(5);
                        dropDownListAccidentIndication.DataValueField = "EMR053_ACCIDENT_INDICATION_ID";
                        dropDownListAccidentIndication.DataTextField = "EMR053_DESCRIPTION";
                        dropDownListAccidentIndication.DataBind();
                        dropDownListAccidentIndication.Items.Insert(0, String.Empty);

                        if (!bool.Parse(GensConfigManager.GetConfig(ConfigOptions.PersonalSettingsAdmissionOptions.MedicalDischarge)))
                        {
                            GensLabeMotivoAlta.Visible = false;
                            GensMaskEditDateMotivoAlta.Visible = false;
                            GensRequiredValidator1.Visible = false;
                            GensRequiredValidator1.Enabled = false;
                            GensMaskEditHrAlta.Visible = false;
                            GensLabeHoraAlta.Visible = false;
                            GensRequiredValidator2.Visible = false;
                            GensRequiredValidator2.Enabled = false;

                            // SE FOR UMA ALTERAÇÃO DE ALTA OU VISUALIZAÇÃO FAZ A CARGA DOS DADOS
                            if (recordState != RecordState.Inserting)
                                LoadDischargeData(dischargeId);
                            else
                            {
                                maskEditDateDischarge.Text = DateTime.Now.ToString("dd/MM/yyyy");
                                maskEditTimeDischarge.Text = DateTime.Now.ToString("HH:mm");
                            }
                        }
                        else
                        {
                            //Carrega os dados com base na alta médica
                            LoadMedicalDischargeData(admissionId);

                            GensMaskEditDateMotivoAlta.Enabled =
                            GensMaskEditHrAlta.Enabled =
                            dropDownListReasonDischarge.Enabled =
                            textBoxBehavior.Enabled =
                            DataGridCID.Enabled =
                            dropDownListCPT.Enabled =
                            HyperLinkInsertCID.Enabled = false;

                            DataGridCID.UrlFormatString = string.Empty;

                            #region Carrega hora atual nos campos

                            DateTime dateNow = DateTime.Now;

                            maskEditDateDischarge.Text = dateNow.ToString("dd/MM/yyyy");
                            maskEditTimeDischarge.Text = dateNow.ToString("HH:mm");

                            #endregion
                        }

Home Dischargeformedit.js

//Validação de Data de Admissão
function ValidateTimeAdmission(source, arguments) {
    var arrayAux = document.getElementById('labelAdmission').innerHTML.split(' ');
    var dateAux = arrayAux[0].split('/');
    var timeAux = arrayAux[1].split(':');
    var AdmissionDate = dateAux[2] + dateAux[1] + dateAux[0] + timeAux[0] + timeAux[1] + timeAux[2];

    arrayAux = (document.getElementById('maskEditDateDischarge').value + ' ' + document.getElementById('maskEditTimeDischarge').value + ':00').split(' ');
    dateAux = arrayAux[0].split('/');
    timeAux = arrayAux[1].split(':');
    var DeathDate = dateAux[2] + dateAux[1] + dateAux[0] + timeAux[0] + timeAux[1] + timeAux[2];

    if (DeathDate > AdmissionDate) {
        source.errormessage = "";
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }

    //Verificação se hora da alta é maior que a data e hora atual 
    var DateNow = new Date();
    DateNow = DateNow.getDate() + '/' + (DateNow.getMonth() + 1) + '/' + DateNow.getFullYear() + ' ' + DateNow.getHours() + ':' + DateNow.getMinutes();

    var CurrentTime = (document.getElementById('maskEditDateDischarge').value + ' ' + document.getElementById('maskEditTimeDischarge').value + ':00').split(' ');

    if (CurrentTime > DateNow) {
        source.errormessage = "";
        arguments.IsValid = true;
    } else {
        arguments.IsValid = false;
    }    
}

1 answer

0

Normalized after making the change below via javascript

Dischargeformatedit.aspx

<gens:GensCustomValidator ID="customValidatorTimeAdmission" runat="server" 
    ErrorMessage="Hora da alta não pode ser menor que hora de admissão ou a data e hora  
    da alta não podem ser maiores que a data e hora atual."  
    ControlToValidate="maskEditTimeDischarge"  
ClientValidationFunction="ValidateTimeAdmission">*</gens:GensCustomValidator>

Dischargeformatedit.js

function ValidateTimeAdmission(source, arguments) {
    var arrayAux = document.getElementById('labelAdmission').innerHTML.split(' ');
    var dateAux = arrayAux[0].split('/');
    var timeAux = arrayAux[1].split(':');
    var AdmissionDate = dateAux[2] + dateAux[1] + dateAux[0] + timeAux[0] + timeAux[1] + timeAux[2];

    arrayAux = (document.getElementById('maskEditDateDischarge').value + ' ' + document.getElementById('maskEditTimeDischarge').value + ':00').split(' ');
    dateAux = arrayAux[0].split('/');
    timeAux = arrayAux[1].split(':');
    var DeathDate = dateAux[2] + dateAux[1] + dateAux[0] + timeAux[0] + timeAux[1] + timeAux[2];

    var DateNow = new Date(),
           anoF = DateNow.getFullYear(),
           mes = (DateNow.getMonth() + 1).toString(),
           mesF = (mes.length == 1) ? '0' + mes : mes,
           dia = DateNow.getDate().toString(),
           diaF = (dia.length == 1) ? '0' + dia : dia,
           hora = DateNow.getHours(),
           horaF = (hora.length == 1) ? '0' + hora : hora,
           min = DateNow.getMinutes(),
           minF = (min.length == 1) ? '0' + min : min;
    var DataHoraFormat = (anoF + '' + mesF + '' + diaF + '' + horaF + '' + minF + '00');

    if ((DeathDate > AdmissionDate) && (DataHoraFormat >= DeathDate)) {
        source.errormessage = "";
        arguments.IsValid = true;
    }
    else {
        arguments.IsValid = false;
    }
}

Browser other questions tagged

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