[Date] field to only allow current date and future dates (No past dates)

imranybs
Kilo Contributor

Hi,

I've the following script that allows not to select any past dates. But I would like to modify this to select the current date and any future dates. Please advise. Thanks ! function onChange(control, oldValue, newValue, isLoading, isTemplate) {                 if (isLoading || newValue == '') {                                 return;                         }                         var d = getDateFromFormat(newValue, g_user_date_format);                 var today = new Date();                 if (d < today) {                                 alert('The date value cannot be before the current date and time. Please correct.');                 g_form.clearValue("effective_date");                 }         }

1 ACCEPTED SOLUTION

arnabwa
Giga Guru

Hi Imran and All,



Let us make this very very simple.


We will make a UI Policy with this condition :


curr1.PNG


here manufacture_date is the field which I'm checking if a past date is not entered. Replace it with your own variable name.



And now we just write a 2 line code in the "Script" part of the same UI Policy :


curr2.PNG



And that's it guys. We are good to go. We need not use any client scripts because for form validations the best practice is to use UI Policies. Using client scripts would have performance issues.


Please let me know if you have further doubts.



Thanks,


Arnab


View solution in original post

25 REPLIES 25

imranybs
Kilo Contributor

Capture 50.PNGHi,


Where can I find [Script Include]? I'm just viewing and working on [Catalogue Client Scripts].


Thanks!


Paramahanns
Tera Expert

Hi,



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


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


return;  


}  


var date = new Date();  


var dateEntered= new Date(getDateFromFormat(g_form.getValue('end_date'),g_user_date_time_format));  


if (dateEntered< date) {  


alert('End date must be in the future.');  


g_form.clearValue('end_date');  


}  



Please see if this helps..



Regards


Param


arnabwa
Giga Guru

Hi Imran and All,



Let us make this very very simple.


We will make a UI Policy with this condition :


curr1.PNG


here manufacture_date is the field which I'm checking if a past date is not entered. Replace it with your own variable name.



And now we just write a 2 line code in the "Script" part of the same UI Policy :


curr2.PNG



And that's it guys. We are good to go. We need not use any client scripts because for form validations the best practice is to use UI Policies. Using client scripts would have performance issues.


Please let me know if you have further doubts.



Thanks,


Arnab


Hey Arnab, the solution didn't work for me. I am on Jakarta.

I have a date field, and set the UI policy as you outlined, but it doesn't show any error if I select any past date. Can you "please" point out what am I doing wrong:

find_real_file.png

The Script:

function onCondition() {


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


       alert("Enter valid date");


}

 

And set the UI Policy {since Variable is under a Variable set, does it make any difference?}

find_real_file.png

ronnie1
Mega Contributor

Hi Arnab,

 

I have a scenario on catalog item where,

when user selects a date for a variable called 'X' and then

there is another variable for example: 'Y' which will be a select box of questions 1,2,3,4.

Based on the selection of this question, the start and end dates will be shown. So if i select '3' for the variable 'Y' then, three start and end dates will be shown and these end dates should not be future date of 'X' variable date selected and the start date should be equal or less than the end date. In this case what should be the complete script to achieve this task? I have no idea as i am very new to such requirement. Could you please help??