Prevent Saturday / Sunday from being selected

billi_lumley
ServiceNow Employee
ServiceNow Employee

Attempting to create a catalog client script to prevent a user from selecting Saturday or Sunday using a Date variable.

Tried using the following to prevent a user from selecting Saturday or Sunday and nothing happens when upon submission...

function onSubmit() {

  var udate = getDateFromFormat(g_form.getValue('move_date'), g_user_date_format);

  var userDate = new Date(udate);

  var day = userDate.getDay();

  if (userDay == 0 || userDay == 6) {

  alert('stop');

  return false;

  }

}

1 ACCEPTED SOLUTION

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Billi,



First things first, let's check out what values we're working with. To make it easy, we'll alert the specific values we care about.



Would you mind updating the function like so, and letting us know the output?



function onSubmit() {  


      var udate = getDateFromFormat(g_form.getValue('move_date') , g_user_date_format);


      var userDate = new Date(udate);


      var day = userDate.getDay();  



      alert("Is move_date a valid field? " + g_form.hasField("move_date") + "\n What is its value? " + g_form.getValue("move_date")


                      + "\n Moving date as integer: " + udate + "\n Moving date readable: " + userDate + "\n Moving day: " + day);


                     


      if (userDay == 0 || userDay == 6) {  


              alert('stop');  


      }


     


      return false;


}




This version should alert a box with 4 question/answer pairs. It will also always return false (the form won't submit), but we will move the return false back to the if condition after we determine why it's not working.



Thanks


Cory


View solution in original post

6 REPLIES 6

coryseering
ServiceNow Employee
ServiceNow Employee

Hi Billi,



First things first, let's check out what values we're working with. To make it easy, we'll alert the specific values we care about.



Would you mind updating the function like so, and letting us know the output?



function onSubmit() {  


      var udate = getDateFromFormat(g_form.getValue('move_date') , g_user_date_format);


      var userDate = new Date(udate);


      var day = userDate.getDay();  



      alert("Is move_date a valid field? " + g_form.hasField("move_date") + "\n What is its value? " + g_form.getValue("move_date")


                      + "\n Moving date as integer: " + udate + "\n Moving date readable: " + userDate + "\n Moving day: " + day);


                     


      if (userDay == 0 || userDay == 6) {  


              alert('stop');  


      }


     


      return false;


}




This version should alert a box with 4 question/answer pairs. It will also always return false (the form won't submit), but we will move the return false back to the if condition after we determine why it's not working.



Thanks


Cory


Thanks Cory.   The output was as follows:



Is move_date a valid field? true


What is its value? 01-16-2016


Moving date as integer: 1452980913000


Moving date readable: Sat Jan 16 2016 16:48:33 GMT-0500 (EST)


Moving day: 6


Hi Billi,



Did you get a second alert dialog that said "stop"? According to the values shown, it correctly identified the moving date as a Saturday. Your conditional should have triggered after clicking "OK" on the first dialog (the one that popped up with those 4 question/answer pairs).



If you saw the second dialog, then that means that your conditional worked. The code you are already using is fine.


No, the second alert did not popup only because I had the incorrect object listed.   Once I updated with day instead of userDay, it worked fine.   Thanks!!!



function onSubmit() {


  var udate = getDateFromFormat(g_form.getValue('move_date') , g_user_date_format);


  var userDate = new Date(udate);


  var day = userDate.getDay();



  if (day == 0 || day == 6) {


  alert('Stop');


  }



  return false;


}