Start Date cannot be in the past

sonita
Giga Guru

What I'm trying to accomplish is that: Start Date cannot be in the past

This is my script:

but my problem is that , I need to be able to select today's date as well, but with this script it doesn't let me .

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

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

          return;

    }

var today = new Date().getTime();

 

  var start = new Date(g_form.getValue('start_date'));

  g_form.hideFieldMsg('start_date', true);

  if(start<today)

  {

  g_form.showFieldMsg('start_date','Start date cannot be in the past','error');

  g_form.clearValue('start_date');

  }

}

1 ACCEPTED SOLUTION

Harneet Sital
Mega Sage
Mega Sage

Write a UI policy with the following condition :



Capture.PNG



Write following script :


function onCondition() {


      g_form.setValue('date_of_joining', '');


      alert("Enter valid date");


}



This is working fine for me. Try it.


View solution in original post

5 REPLIES 5

jbauguess
Tera Expert

Try calling "getTime()" on your start variable:



var start = new Date(g_form.getValue('start_date')).getTime();


rajeevlochan82
Mega Guru

Hi,



I created a Script include to achieve this functionality.



1) Create a Script include with name 'CompareDateTime' and following code



var CompareDateTime = Class.create();


CompareDateTime.prototype = Object.extendsObject(AbstractAjaxProcessor, {


      compareDate: function () {


              var answer = "false";


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


              var datetwo = gs.nowDateTime();


  answer = gs.dateDiff(dateOne,datetwo, true);



  if(answer >= 0){


              return 'false' ;


  }



2) Now create a onchange client script on change of your datetime field. In my below example the datetime field name is 'u_termination_date'



var dt1 = g_form.getValue('u_termination_date');


var glideAJ = new GlideAjax('CompareDateTime');


glideAJ.addParam('sysparm_name' , 'compareDate');


glideAJ.addParam('sysparm_date', dt1);


glideAJ.getXML(callBack);



function callBack(response){


  var answer = response.responseXML.documentElement.getAttribute("answer")


  if(answer =='false')


  {


    g_form.showErrorBox('u_termination_date' , 'Termination date time should be greater than current date time');


  }


  }


Michael Fry1
Kilo Patron

You should be able to use: var start = g_form.getValue('start_date')


Harneet Sital
Mega Sage
Mega Sage

Write a UI policy with the following condition :



Capture.PNG



Write following script :


function onCondition() {


      g_form.setValue('date_of_joining', '');


      alert("Enter valid date");


}



This is working fine for me. Try it.