0

I am working with a Task list in SharePoint online. On the new item form, I have a field called 'Due Date' (Internal name: DueDate) which is set to optional and for one of the Flow requirements, I have to keep it optional as upon making it required my existing flow logic will break.

So i am looking to see if validation can be added using JS. Basically when a user clicks on Save button on the new item form, the code should check if Due date field is populated or not, if not then it should show a message in red that you cannot leave due date empty. Once they populate, it should let them save.

Can anyone help me with the JS code. P.S: Again I do not want to leverage OOTB required approach here, need a JS solution please.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function PreSaveAction(){
    var dueDate=$("input[title='Due Date']").val();
    var entity = $("select[title='Entity ID Name']").val();


    if(dueDate.trim()==""){
        alert("Please select a Due Date.");
        return false;
    }
    if(entity.trim()==""){
    alert("Please populate Entity ID Name.");
    return false;
    }


    var getIDPeoplePicker=$("div[title='Assigned To']").attr("id");
    var ppobject = SPClientPeoplePicker.SPClientPeoplePickerDict[getIDPeoplePicker];
    var usersobject = ppobject.GetAllUserInfo();
    if(usersobject.length==0){
        alert("Please select a user in Assigned To field.");
        return false;
    }
    return true;
}
</script>

enter image description here

Thanks in advance.

2 Answers 2

3

We can add the code below into a script editor web part in task list form page to achieve it.

<script src="https://code.jquery.com/jquery-1.12.4.min.js" type="text/javascript"></script>
<script type="text/javascript">
function PreSaveAction(){
    var dueDate=$("input[title='Due Date']").val();
    if(dueDate.trim()==""){
        alert("Please select a Due Date.");
        return false;
    }
    if(!$("div[title='Assigned To']").find(".sp-peoplepicker-initialHelpText").is(":hidden")){
        alert("Please select a user in Assigned To field.");
        return false;
    }
    return true;
}
</script>

enter image description here

8
  • Thanks a lot LZ_MSFT. I also have a choice column called Entity and a people picker 'Assigned To, will the same logic work for these fields as well? PLease advise.
    – mdevm
    Apr 8, 2019 at 13:31
  • P.S: Works great for a choice column as well, I just select instead of Input. Can you please assist what needs to be used for AssignedTo people picker field?
    – mdevm
    Apr 8, 2019 at 13:43
  • I have modified the code in the reply to make it works for the Assigned To field.
    – LZ_MSFT
    Apr 9, 2019 at 1:21
  • I tested the code for assigned to field but its not working. Can you please assist. Thanks.
    – mdevm
    Apr 9, 2019 at 14:48
  • Using F12 to check if some errors messages occurs, in my test environment, it working fine.
    – LZ_MSFT
    Apr 10, 2019 at 1:18
0

you can achieve the validation for People Picker field through the following code

 <script type="text/javascript">
   function PreSaveAction()
   {
     //assign value of people picker field to variable
     var AssignedTo = "Assigned To";
      Assigned = SPClientPeoplePicker.SPClientPeoplePickerDict[$('[title="' + AssignedTo + '"]')[0].id];

  //check if the variable contains values
   if (Assigned.TotalUserCount == 0) {
    alert("Please, Enter at least one User in Assigned To.");
    return false;
      }
        return true;
      }
   </script>
2
  • It does not work
    – mdevm
    Apr 12, 2019 at 14:00
  • i have updated my answer. Apr 15, 2019 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.