1

For one of the requirements, I am working with SharePoint online and part of that, I would like to check if a choice column is empty or not using JSOM. Can someone please help me with the code. If a choice column is empty then I would like to restrict the user to perform Save/Upload in a SharePoint document library and show some message that user needs to fill out these fields. There are about 3 choice columns which I would like to validate if they are empty or not.Only and only if all three of them are non- empty then user can do upload or save in a document library. So this code will be added to the edit form of a Sp library.

I have 3 content types in the library, out of which one of those have these three columns so I would want to make sure that validation check only occurs for these 3 columns that are part of a content type, let's say test.

Thanks in advance.

1 Answer 1

2

Hi you can use PreSaveAction function to validate the item.

   $(document).ready(function() {
       ProducerReferral();
       attachEventHandlers(); // function for checking the duplication of files  
       $('input[value=Save]').click(function() {
           PreSaveTest();
       });
   });

   function PreSaveTest() {
       PreSaveAction();
   }

   function PreSaveAction() {
       if ($("#idAttachmentsRow").css("display") == "none") {
           $("#part1 > h4")[1].innerHTML += "<span style='margin-left: 40px;' class='ms-formvalidation ms-csrformvalidation'>Please Attach Files.</span>";
           returnVal = false;
       } else {
           return true;
       }
   }

Or

    <script type="text/javascript">
 var $j = jQuery.noConflict(); 
 function PreSaveAction() { 
   alert("here");
   var txtStatus = $j(":input[title='Status']").val(); 
   if(txtStatus == "In Progress"){        
   alert("Invalid Status");        
   var statusfocus = $j(":input[title='Status']");        
   statusfocus .focus();        
   return false;}
   return true;
  }
</script>

Refer PreSaveAction

All the best !!!!

2
  • Hello Shridhar, i can not make them required since I want the other functionality to work and it depends on columns being non-mandatory and which is why I need to know what would be the JS approach, thanks.
    – mdevm
    Commented May 15, 2018 at 19:06
  • Hi mdevm, you can use PreSaveAction for choice column validation Commented May 18, 2018 at 11:05

Your Answer

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

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