Validate the date field to check whether the entered date is more than 2 years past date

Are Kaveri
Tera Contributor

Hi 

 

I have below requirement 

 

there is the below date field 

Applicant service date( the service of the applicant must be more than 2 years , if it is less than 2 years please don’t allow to enter)

there is another field called Date of joining field of date type 

 

how to validate below Applicant service field ?

 

anyone has any information please share…

1 ACCEPTED SOLUTION

Vishal Birajdar
Giga Sage

Hi @Are Kaveri 

 

May be below solution work for you...!!

 

So here I have done it on incident table on "Actual start" & "Actual end" Fields

You can use your field values here.

 

Step 1 : Create onChange client script on "Actual end" field 

 

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

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

        return;

    }

    if (g_form.getValue('work_start')) {

        var ga = new GlideAjax("DateTimeUtil");   //script include name

        ga.addParam('sysparm_name', "getDate"); //function name

        ga.addParam('sysparm_date', g_form.getValue('work_start'));

        ga.getXMLAnswer(callBack);

    }

    function callBack(answer) {

        if (newValue < answer) {

            g_form.showFieldMsg("work_end", "Please enter date more than 2 years from start date");

            //may be you can clear value of field here

        }

    }

}

 

Step 2 : Write Client Callable Script Include 

Name : DateTimeUtil

 

getDate: function() {

        var date = this.getParameter('sysparm_date');

        var gdt = new GlideDateTime(date);

        gdt.addYearsLocalTime(2);

        return gdt;

    },

 

Output : 

VishalBirajdar7_0-1694410479701.png

 

 

 

 

 

  

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

View solution in original post

4 REPLIES 4

Manmohan K
Tera Sage

Hi @Are Kaveri 

 

You can make use of UI policies to validate date fields.

The below article has step by step information on how to use UI policy to validate dates on form

https://www.servicenow.com/community/developer-articles/no-code-date-validations-through-catalog-ui-...

@Manmohan K  my requirement is 

The Duration between Date of joining and Applicant service date should be more than 2 years.

if it is less than 2 years from Applicant service date  to Date of joining. we should not user to enter and throw some alert.

Date of joining must be less than Applicant service date.

 

Thank you

 

Vishal Birajdar
Giga Sage

Hi @Are Kaveri ,

 

Can you please let us know , which date (I mean Current date or Joining date) you are referring to validate Application service date.

 

i.e. you want to Application service date to be more than 2 years from current date or Joining date

 

 

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates

Vishal Birajdar
Giga Sage

Hi @Are Kaveri 

 

May be below solution work for you...!!

 

So here I have done it on incident table on "Actual start" & "Actual end" Fields

You can use your field values here.

 

Step 1 : Create onChange client script on "Actual end" field 

 

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

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

        return;

    }

    if (g_form.getValue('work_start')) {

        var ga = new GlideAjax("DateTimeUtil");   //script include name

        ga.addParam('sysparm_name', "getDate"); //function name

        ga.addParam('sysparm_date', g_form.getValue('work_start'));

        ga.getXMLAnswer(callBack);

    }

    function callBack(answer) {

        if (newValue < answer) {

            g_form.showFieldMsg("work_end", "Please enter date more than 2 years from start date");

            //may be you can clear value of field here

        }

    }

}

 

Step 2 : Write Client Callable Script Include 

Name : DateTimeUtil

 

getDate: function() {

        var date = this.getParameter('sysparm_date');

        var gdt = new GlideDateTime(date);

        gdt.addYearsLocalTime(2);

        return gdt;

    },

 

Output : 

VishalBirajdar7_0-1694410479701.png

 

 

 

 

 

  

Vishal Birajdar
ServiceNow Developer

I know one thing, and that is that I know nothing.
- Socrates