How to use PreSaveAction() in newform.aspx in SharePoint Online/2013/2016 List

PreSaveAction() in NewForm.aspx in SharePoint 2013 list

In this post we will discuss how to use PreSaveAction() method in SharePoint list form in SharePoint 2013/2016/Online. We will see how to use PreSaveAction() in newform.aspx in SharePoint Online/2013/2016.


PreSaveAction() a JavaScript function which helps us to do something before the item will be saved. Suppose in the list form you want to do some validation check when a user clicks on the submit button then PreSaveAction() will be very much helpful. This method called before the form is submitted.

You can use PreSaveAction() in NewForm.aspx or EditForm.aspx in any SharePoint Online/2013/2016 list.

Example of using PreSaveAction() in SharePoint List

In this example, I have a SharePoint list which has a Title column which is a mandatory field. Now I am using PreSaveAction() JavaScript function to check whether Title is blank or not. If it is blank then it will display an alert message and it will not save the item. If Title is not blank then it will Save the item

Here I have using jQuery to retrieve Title column value, but you can use SPUtility.js to retrieve the field value as well.

I have edited the NewForm.aspx and put the below code inside a Script editor web part.

<script src=”http://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js”></script>
<script type=”text/javascript”>
var j = jQuery.noConflict();
function PreSaveAction() {
var txtTitle = j(“:input[title=’Title’]”).val();
alert(txtTitle);
if(txtTitle == ”){
alert(“Please enter a title”);
return false;
}
else{
return true;
}
}
</script>

Save the page and try to add an item to the SharePoint list. If your title is blank and you click on the Save button then it will show the alert like below:

presaveaction sharepoint online
presaveaction sharepoint 2013

You may like following SharePoint tutorials:



In the same way, you can use PreSaveAction() in editoform.aspx page in SharePoint Online list. Hope this article will be helpful to you, to know more about how you can use presaveaction() in SharePoint 2013/2016/Online.

Check out Best Alternative to InfoPath -> Try Now

You May Also like the Following SharePoint Online Tutorials:

About Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SharePointSky.com

View all posts by Bijay Kumar →