I have the following 2 datetime fields which are used in my custom page layout on my publishing site. Basically, when a user is editing the page I want to validate that the 'NewsLiveDate' < 'NewsArchiveDate'.

<SharePoint:DateTimeField FieldName="NewsLiveDate" runat="server" id="News_ArticleStartDate ></SharePoint:DateTimeField>    
<SharePoint:DateTimeField FieldName="NewsArchiveDate" runat="server" id="News_ArticleEndDate" ></SharePoint:DateTimeField>

I know I can provide the PreSaveAction() javascript function, but that is ONLY valid in Newform.aspx and editform.aspx. I have written event receiver code but this is all server side. The client wants client-side validation. I don't know how to intercept the save process.

How do I go about this in the page layout?

4 Answers 4


You can register form submit code using Page.ClientScript.RegisterOnSubmitStatement method. There you can make JS validation and prevent submission of the form. Please check my blog post for more info.

  • Link does not work Commented Feb 26, 2020 at 14:31

I've found two methods so far...

The first is to hijack the WebForm_OnSubmit javascript function which means I can just put some js in the page layout which is a bit of a hack.

var form = WebForm_OnSubmit; 
WebForm_OnSubmit = function () { 
isValid = ValidateBeforeSubmit(); // Custom function doing jQuery validation on form. Returns true/false
if (isValid) return form.apply(this, arguments); 
   else { 
    jQuery('span[id*=notification_]').remove(); //removes the default 'Saving...' notification 
   return false; } 

As proposed by @Yuri: Create a page based on PublishingLayoutPage. Override the page load method insert your custom js function which will execute prior

Page.ClientScript.RegisterOnSubmitStatement(this.GetType(), "validate_page", " if (!validate()) { return false; }; ");

Thanks Yuri. I think your method is the cleanest.

  • You can just create a regular asp.net control (with registration of Submit Statement) and put it to a page layout. Commented Apr 9, 2013 at 11:08

You can use the validation settings on the pages library. Navigate to library settings and then click on 'Validation Settings'. It allows you to validate the fields based on the provided formula. You can provide formula something like =[Date1]<=[Date2].


[EDIT]: In order to answer the question as clarified in the comments I did some deeper digging and found this guy: http://sadomovalex.blogspot.com/2011/05/publishing-pages-auto-save-mechanism-in.html

At the very bottom of the page there is a reference to the Ribbon Javascript API (SP.Ribbon.PageState.PageStateHandler). SharePoint registers against the browser's OnPreRender() method the following:

window.onbeforeunload = cms_handleOnBeforeUnload;

You would add a CEWP that overrides cms_handleOnBeforeUnload and use your own implementation. The example and blog post are specific to the 2010 publishing page's auto-save functionality (though using the 2007 UI [v3]) but the important part is:


For us only 2 lines of code are important:

1.) WebForm_OnSubmit();

2.) ...

3.) WebForm_DoCallback('ctl00$SPNavigation$ctl01$publishingConsoleV3$sbn1', '', SBN_CallBackHandler, null, null, false);

First line (WebForm_OnSubmit()) is exactly the line which causes saving of the page content. You can try to override the cms_handleOnBeforeUnload with your realization and comment the first line – you will see that changes now are not saved when user leaves the edit mode. And second line – is call to SaveBeforeNavigationControl.GetCallbackResult() method. If you remember this method returns "save succeeded" string. So cms_handleOnBeforeUnload handler checks that if returned string is not "save succeeded" – method returns string with validation errors. And this message will be shown in the middle of the IE javascript window as we already saw above (http://msdn.microsoft.com/en-us/library/ms536907(v=vs.85).aspx).


I would recommend looking into the v4 UI implementation of the Ribbon API.

Good luck (:


Might I recommend you try the following jQuery plugin? http://www.datejs.com/

If you check the site you will see a live demo so you should be able to get some ideas for real-time date validation. From the site, there are some crazy syntactic sugars available:

// What date is next thursday?

// Add 3 days to Today

// Is today Friday?

// Number fun

// 6 months from now
var n = 6;

// Set to 8:30 AM on the 15th day of the month
Date.today().set({ day: 15, hour: 8, minute: 30 });

// Convert text into Date
Date.parse('t + 5 d'); // today + 5 days
Date.parse('next thursday');
Date.parse('February 20th 1973');
Date.parse('Thu, 1 July 2004 22:30:00');
  • I know how to do javascript date validation. The question is how to perform the validation when a user clicks on any of the save mechanisms in Sharepoint in a publishing page layout
    – Ralph W
    Commented Apr 4, 2013 at 8:51
  • My recommendation, though not stated very clearly, was to use the datejs.com site as an example. See the demo textbox and what items it could handle? Notice that it validates on-the-fly? You can have the textbox on your publishing page validate itself upon entry of data just like their example does. Not sure why that deserved a down vote ):
    – Kristopher
    Commented Apr 4, 2013 at 14:03
  • Because it doesn't stop the user from clicking save and saving the value. Javascript validation should prevent the page from posting the values. This just shows a message. No offence for the downvote. Its just that its not the answer I'm looking for.
    – Ralph W
    Commented Apr 4, 2013 at 14:06

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.