Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

Validate two date fields and make field visible and mandatory

Kruthik M Shiva
Tera Contributor

Hi All,

I have a requirement to validate two date fields when requestor fills the form if the second date field is more than 2 days of first date field a new field should get visible and make mandatory to fill the reason. How to do this and please do provide script required for this .

Thanks in advance.

4 REPLIES 4

Jitendra Diwak1
Kilo Sage

Hi @Kruthik M Shiva,

 

Please try this below code:

function onChange(control, oldValue, newValue, isLoading) {

    if (isLoading || newValue === '') {

        return;

    }

 

    var firstDate = g_form.getValue('first_date_field');

    var secondDate = g_form.getValue('second_date_field');

    var thirdField = g_form.getControl('third_field');

 

    // Convert date strings to Date objects

    var firstDateObj = new Date(firstDate);

    var secondDateObj = new Date(secondDate);

 

    // Calculate the difference in days

    var timeDiff = Math.abs(secondDateObj.getTime() - firstDateObj.getTime());

    var diffDays = Math.ceil(timeDiff / (1000 * 3600 * 24));

 

    // If the difference is more than 2 days, show and make mandatory the third field

    if (diffDays > 2) {

        thirdField.setAttribute('mandatory', true);

        g_form.showFieldMsg('third_field', 'Please provide a reason for the difference.', 'info');

    } else {

        thirdField.removeAttribute('mandatory');

        g_form.clearMessages(' third_field');

    }

}

 

Please accept my solution if it resolves your issue and thumps 👍 up

 

Thanks 

Jitendra 

Please accept my solution if it works for and thumps up.

Hi Jitendra,
Thanks for the response I will check the script and Implement I will be accepting once it worked.
Thanks,
Kruthik Shivaprasad

Astik Thombare
Tera Sage
Tera Sage

Hi @Kruthik M Shiva ,

 

Try below code 

 

 

 

 

function onChange(control, oldValue, newValue, isLoading, isTemplate) {

    if ( newValue === '') {
        g_form.setMandatory('u_reason', false);
        g_form.setDisplay('u_reason', false);
        return;
    }

    // Get the values of start_date and end_date fields
    var startDate = g_form.getValue('u_start_date');
    var endDate = g_form.getValue('u_end_date');

    // Ensure both dates are filled
    if (startDate && endDate) {
        // Convert to Date objects
        var start = new Date(startDate);
        var end = new Date(endDate);

        // Calculate the difference in days
        var timeDiff = end - start;
        var daysDiff = timeDiff / (1000 * 3600 * 24);

        // Check if the difference is more than 2 days
        if (daysDiff > 2) {
            // Show and make the reason field mandatory
            g_form.setDisplay('u_reason', true);
            g_form.setMandatory('u_reason', true);
        }else{
 g_form.setMandatory('u_reason', false);
        g_form.setDisplay('u_reason', false);
		}
    }

}

 

 

 

 

I have tested it in my PDI and it is working fine

 

 

For demo I have created 3 fields on incident form - Start Date, End Date and Reason

 

Result -

 

Scenario 1 - When Form loads and start and end date is empty

AstikThombare_0-1718182987296.png

Scenario 1 - When date diff is less than 2 days

 

AstikThombare_1-1718183115137.png

 

As date diff is less than 2 days . the reason field will be hidden 

 

Scenario 3 - when date difference is more than 2 days 

AstikThombare_2-1718183190084.png

Even if you changed the end date later and if then date diff is less than 2 days then it will be hidden again 

 

 

         If my reply helped with your issue please mark helpful 👍 and correct ✔️ if your issue is resolved.

 

                         By doing so you help other community members find resolved questions which may relate to an issue they're having

 

 

Thanks,

 

Astik