i want to validate date is past or not if past want to display error msg using g_form.showfieldmessage

gomathysanjana
Mega Expert

i   want to validate date is past or not if past want to display error msg using g_form.showfieldmessage

1 ACCEPTED SOLUTION

thameem1
Kilo Expert

Hi


You can check using Client Script On Change calling a script include where u can check the date with current date


Example Code:


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


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


                    return;


              }


        var ga = new GlideAjax('DateandTIme');


            ga.addParam('sysparm_name', 'getcurrentdate');


            ga.addParam('sysparm_Publised', newValue);


            ga.getXML(UpdatePublished);


     


        }


function UpdatePublished(response, newValue){


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


            if(answer){


          // g_form_show field msg


            g_form.clearValue('published');


            return;


            }


}



Script include :



var DateandTIme = Class.create();


DateandTIme.prototype = Object.extendsObject(AbstractAjaxProcessor, {



  getcurrentdate : function(){


  var publisheddate = this.getParameter('sysparm_Publised');


if(publisheddate < gs.nowDateTime()){


  return true;


  }


  else


  return false;


  },


      type: 'DateandTIme'


});


View solution in original post

9 REPLIES 9

Ankur Bawiskar
Tera Patron
Tera Patron

Hi Gomathy,



Have an onChange client script on the date field. Validate if date is past than current date then show the message.



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

amlanpal
Kilo Sage

Hi Gomathy,



You can achieve this by a Client Script and one Script include called inside the client script. Please find the code for those below:



Client Script:


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


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


  return;


  }


  var ga = new GlideAjax('DateValidations ');


  ga.addParam('sysparm_name','validateDate');


  ga.addParam('sysparm_date',newValue);


  ga.getXML(ProcessResult);




  function ProcessResult(response) {


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


    if (answer < 0)


  {


  g_form.clearValue('extend_trim_access_date');


  g_form.showFieldMsg('field_name', 'Please enter a value greater than or equal to Todays date', 'Error');


  }


  else {


  g_form.hideFieldMsg('extend_trim_access_date', true);


  }


    }


}



Script Include:


var DateValidations = Class.create();


CatalogItemDateValidations.prototype =   Object.extendsObject(AbstractAjaxProcessor, {



  //Creating new Function named 'validateDate'



  validateDate: function() {


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


    return gs.dateDiff(gs.now(),ActualStartDate, true)/86400;


  },


type: 'CatalogItemDateValidations'


});




I hope this helps.Please mark correct/helpful based on impact



gomathysanjana
Mega Expert

Hi Pal,



This is what I have written




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


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


          return;


    }




    //Type appropriate comment here, and begin script below


    var date_obj = new Date(getDateFromFormat(newValue,user_date_time_format));


  var current_date=new Date();


  if(date_obj<current_date){


  g_form.showFieldMsg('Date','Date should not be past value',error,true);


  }


  else{


  g_form.setValue('Date',current_date);


  }


}


Thank U its working