Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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?

share|improve this question
add comment

4 Answers

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.

share|improve this answer
    
Thanks Yuri, this is what I was looking for. –  Grooverinthesouth Apr 9 '13 at 9:38
add comment

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].

share|improve this answer
add comment

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.

share|improve this answer
    
You can just create a regular asp.net control (with registration of Submit Statement) and put it to a page layout. –  Yuri Leontyev Apr 9 '13 at 11:08
add comment

[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 (:

[/Edit]

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?
Date.today().next().thursday();

// Add 3 days to Today
Date.today().add(3).days();

// Is today Friday?
Date.today().is().friday();

// Number fun
(3).days().ago();

// 6 months from now
var n = 6;
n.months().fromNow();

// 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('today');
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');
share|improve this answer
    
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 –  Grooverinthesouth Apr 4 '13 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 ): –  Wartickler Apr 4 '13 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. –  Grooverinthesouth Apr 4 '13 at 14:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.