Client Side Javascript Validation in SharePoint Custom Edit Forms
Recently for a client engagement I used a Custom Field Iterator to control the edit and display form for a content type (more on this in an upcoming post). The requirement was for different fields to be made editable based on the state the list item was in. After deployment, the client requested that an enhancement be made such that the records could not be saved unless at least one of a series of checkboxes was checked.I wanted to use client side validation rather than handling the updating event. After digging around I found http://edinkapic.blogspot.com/2007/10/add-javascript-date-validation-into.html - which led me to PreSaveAction(). The PreSaveAction function is called from PreSaveItem(), which is defined in form.js which is loaded on SharePoint pages. If the PreSaveAction Method returns TRUE then the OnSubmit handler is called otherwise the submit action is cancelled. Unfortunately, there can be only one PreSaveAction method per page, and so co-ordination is required if multiple controls require its use.To add my script file to the page,
I used the ASP.Net Client Script Manager. I added the script from the OnPreRenderMethod() of my custom form iterator using:
if (ControlMode == SPControlMode.Edit)
{
// if this is an edit page add script file for client side checking.
this.Page.ClientScript.RegisterClientScriptInclude("syrinxeditjsfile", "/_layouts/syrinx/syrinxtaskedit.js");
}The Javascript file was then deployed to the 12\TEMPLATES\LAYOUTS\syrinx folder using a feature.