Using PreSaveItem to add custom validation to SharePoint list form

Using PreSaveItem to add custom validation to SharePoint list form

In the past few days I published a new article on the Italian SharePoint Community web site about using PreSaveItem javascript function to add custom validation to SharePoint list forms. It’s written in Italian, you can find it here.

In this article I explain how to change the standard server side to a custom client side form validation. You can’t really change for server to client side, but you can validate your input with JavaScript before to send them to the server and then “skip” server side validation. You can do all of this by using a standard JavaScript function used on every submit button, PreSaveItem.

Out-of-the-box PreSaveItem doesn’t do anything, but on every form save action, SharePoint checks if PreSaveItems return false. In this case it doesn’t allow the form submit. See the following row to better understand what I mean.

In a custom javascript file you have to redefine the PreSaveItem function, adding your custom data validation. In my article I wrote a function that leverage the power of regular expressions to validate an email address field. Let’s see the example below.

I used jQuery to select the field and to add an error message after the input when needed. I also added another little piece of my heart code to clear the error message when the user change the fields falue.

Enjoy!
-Riccardo

 

Submit a Comment