Welcome to Community Week 2025! Join us to learn, connect, and be recognized as we celebrate the spirit of Community and the power of AI. Get the details  

client script

patilabhilash
Tera Contributor
1st:
function onSubmit() {
   //Type appropriate comment here, and begin script below
   var getJoiningDate=g_form.getValue('u_joining_date'); //get the value of the Joining Date
    var todayDate= new Date();  // Create a Date object for today's date
   var getJoiningDateFormat=new Date(getJoiningDate); //Convert the Joining Date to Date obj
   if(getJoiningDateFormat>todayDate)//Check if the Joining Date is greater than todays date
   {
    g_form.addErrorMessage('Joining date date cannot be in future');
    g_form.clearValue('u_joining_date');
    return false;
   }
}
 
2nd :
function onSubmit() {
    //Type appropriate comment here, and begin script below
    var joiningdate = g_form.getValue('u_joining_date');
    var todaydate = new Date();
    var joindateformat = new Date(joiningdate);
    if (joindateformat>todaydate)
    {
        g_form.showFieldMsg('u_joining_date','Joining date date cannot be in future','error');
        g_form.clearValue('u_joining_date');
        return false;
    }
}
1st one is working fine but not second one why? especially this g_form.showFieldMsg('u_joining_date','Joining date date cannot be in future','error');
2 REPLIES 2

Chaitanya ILCR
Mega Patron

Hi @patilabhilash 

 

That's because the order of showFieldMsg and clearValue methods

 

first use 

clearValue and then the showFieldMsg and check

 

 g_form.clearValue('u_joining_date');
 g_form.showFieldMsg('u_joining_date', 'Joining date date cannot be in future', 'error');
 
 
coming the date validation refer the already solved problems in case of yours is not working
 
 
 

Please mark my answer as helpful/correct if it resolves your query.

Regards,
Chaitanya

Can confirm that this is the correct solution! ServiceNow even has a KB article on it: