Developers can face an issue when our client wants to have an action done on a document library on a save click.

Document Library uploads the document, and to validate something on the save click is tricky as the save bypasses the validation and gets saved.

Solution

The solution to do any JavaScript validation is Pre Save action. So here is what you need to do.

Open your Document Library

Under the ‘Library’ tab, click on the Form Web Parts.

form

Choose Default Edit Form, you will see the screen below.

main

Click Add a Web Part,

add

Choose Script Editor, under the Media and Content Web part.

script

Click Edit Snippet, you will see the screen below.

embed

Use the code below and click OK.

Code:

<script src="/jquery-1.8.3.min.js" type="text/javascript"></script> 

<script src="/jquery.SPServices-0.7.2.min.js" type="text/javascript"></script> 

<script type"text/javascript">  $(document).ready(function() 

});  function PreSaveAction() 

</script> 

Use the function PreSaveAction() and paste your validation under the function on the code.

Before saving the document library the code will check your validation and will get saved.

a

Doesn’t it solve a lot of queries related to document libraries functions? Keep learning!