need catalog variable date to validate it is in future

karendk
Kilo Explorer

I've seen this in the community where this is working for others, but it's not working for me.   What do I have wrong?   I get no error message.   It's just ignored.

function onChange(control, oldValue, newValue, isLoading) {
if (isLoading || newValue == '') {
return;
}

if(newValue != '')
{
var reqDate = new Date(getDateFromFormat(newValue, g_user_date_format));
var today = new Date();

if(reqDate.getTime() <= today.getTime()) {

alert('Date must not be in the past.');
  g_form.setValue('sDate', '');
}
}
}

1 ACCEPTED SOLUTION

Tanaji Patil
Tera Guru

Hi Karen,



You are just comparing the time. You should first compare date and then time. Or both at the same time.


Try with the below shown code-



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


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


  return;


  }



// if(newValue != '') don't need this as you already have checked above


// {


  var reqDate = new Date(newValue);


  var today = new Date();


  if(reqDate <= today){


  alert('Date must not be in the past.');


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


  }


// }


}



Thanks,


Tanaji


View solution in original post

16 REPLIES 16

Tanaji Patil
Tera Guru

Hi Karen,



You are just comparing the time. You should first compare date and then time. Or both at the same time.


Try with the below shown code-



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


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


  return;


  }



// if(newValue != '') don't need this as you already have checked above


// {


  var reqDate = new Date(newValue);


  var today = new Date();


  if(reqDate <= today){


  alert('Date must not be in the past.');


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


  }


// }


}



Thanks,


Tanaji


Use GlideAjax for more flexibility and for better precision.


Check out Client Script Date/Time Functions.


A change needed as its a variable.


Replace line


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


with


g_form.setValue('variables.sDate','');


I found it. I had forgotten to populate the Variable Name field on the top section of the Catalog Client Script form.   No wonder it was ignoring the script...   beginners mistake.   Thanks everyone for the help.   It's now working.    


Perfect


Can you please mark the response if this answers your question.