Unable to Select the Current date

msm4
Mega Guru

Hi Team,

Please find the below snippet i used to validate the date field to select only the current and future date.

But the problem is im unable to select the current date.

Please guide where i m wrong.

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

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

          return;

    }

var today = new Date();

var dateMS = today.getTime();

var current_date = new Date();

current_date.setTime(dateMS);

var new_date = new Date(getDateFromFormat(newValue, g_user_date_time_format));

g_form.setValue('ftr_needed_by',formatDate(current_date,g_user_date_time_format));

if(new_date>=current_date)

{

// this is valid

}

  else{

  alert("selected date should be current date or future date");

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

}

}

Thanks in Advance

1 ACCEPTED SOLUTION

Harneet Sital
Mega Sage
Mega Sage

Hi,



Rather than client script use the following UI policy with the following condition :



Capture.PNG



Write following script :


function onCondition() {


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


      alert("Enter valid date");


}



Its simple and works in all the cases.


View solution in original post

10 REPLIES 10

Chuck Tomasi
Tera Patron

Hi Smitha,



Here's one I use to validate start/end dates so start < end and start > now.



function onSubmit() {


    var jsStartDate = new Date(g_form.getValue('work_start'));


    var jsEndDate     = new Date(g_form.getValue('work_end'));


    var jsToday         = new Date();


   


    if (jsStartDate < jsToday) {


          alert(getMessage('loaner_error_start_before_today'));


    return false;


    }


   


    if (jsEndDate < jsStartDate) {


          alert(getMessage('loaner_error_end_before_start'));


    return false;


    }


    return true;    


}


msm4
Mega Guru

Hi Tomasi,



thanks for the response.


I would like wrikte a script on OnChange.



could you please guide me in that.


Geoffrey2
ServiceNow Employee
ServiceNow Employee

Hi smitha,



This is the script I normally use for this type of thing.   I actually have the getJSDate() function in a Global UI Script so I can call it from any Client Script.



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


      if (isLoading)


              return;



      var fieldName = 'date';


      g_form.hideFieldMsg(fieldName, true);



      if (!newValue)


              return;



      function getJSDate(dateStr) {


              var usersDateFormat = g_user_date_time_format.split(' ')[0];


              var parts = newValue.split(/[\-\.\/]/);


              var day, month, year;


              if (usersDateFormat.match(/^dd/)) {


                      // little-endian


                day = parts[0];


                      month = parts[1];


                      year = parts[2];


              } else if (usersDateFormat.match(/^yyyy/)) {


                      // big-endian


                      year = parts[0];


                      month = parts[1];


                      day = parts[2];


              } else {


                      // middle-endian


                      month = parts[0];


                      day = parts[1];


                      year = parts[2];


              }


              return new Date(year, month-1, day);


      }



      var now = new Date();


      var todayStart = new Date(now.getFullYear(), now.getMonth(), now.getDate());


      var newDate = getJSDate(newValue);



      if (newDate.getTime() < todayStart.getTime()) {


              g_form.showFieldMsg(fieldName, 'Please pick a date in the future', 'error');


      }


}



It's creating a JavaScript Date object from newValue.   The todayStart is today's date at midnight (without any time added).   Without this you cannot select today's date.


I use field messages instead of alert() popups because I find them to be a smoother user experience.


I'm sure you can modify this for your 2 data fields.   Just called getJSDate for each of your fields.



One thing to be conscious of is the client-side Date() is getting the date from the User's browser, not the ServiceNow system time or the user's ServiceNow time zone. To if the user's browser time setting is different to their ServiceNow time settings, then you could get issues. The way to get around this is to use GlideAjax to make a server call and perform your date validation checks on the server using GlideDateTime with the ServiceNow time settings.


I recommend to use GlideAjax in your onChange client script. Go thru this link for more information on GlideAjax


GlideAjax - ServiceNow Wiki



onChange client script:


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


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


          return;  


  }  


var ga = new GlideAjax('DateValidation');  


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


ga.addParam('sysparm_date',newValue);  


ga.getXML(CallBack);  


 


function CallBack(response) {  


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


  if(answer=='false'){  


      alert("Invalid date");  


      g_form.setValue('<your date/time field name>','');  


  }  


}  


}  




Script Include:


Name:DateValidation


Client callable: true


Script:


Script Include:


Name:DateValidation


Client callable: true


Script:


var DateValidation = Class.create();  


DateValidation.prototype = Object.extendsObject(AbstractAjaxProcessor, {  


  validateDate: function(){  


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


  if(gs.dateDiff(gs.nowDateTime(),sel_date,true)<0){  


  return false;  


  }  


  return true;  


  },  


  type: 'DateValidation'  


});