SharePoint 2010 Validation using Javascript


Hi,

Recently I had to do a custom validation for a Task Edit form based on task the user is performing. For example, if user chooses to Reject the proposal he needs to enter remarks else the remarks are not mandatory.

I had a look at the Save button on the Default Edit form, using the IE Developer tool and its “onclick” event looked something like this.

onclick=”if (!PreSaveItem()) return false;WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(“9440_59e98$ctl00$toolBarTbl$ctl00$diidIOSaveItem”, “”, true, “”, “”, false, true))”

SharePoint checks for the PreSaveItem function before calling the save method. If the value returned from PreSaveItem is false it simply returns without doing anything.

After searching for PreSaveItem function, I found it in the ~\Program Files\Common Files\Microsoft Shared\Web Server Extensions\14\TEMPLATE\LAYOUTS\1033 folder\FORM.js file. The PreSaveItem function had the below definition.

function PreSaveItem()
{
ULSopi:;
alert('Inside PreSaveItem');
debugger;
if("function"==typeof PreSaveAction)
	return PreSaveAction();
return true
}

So the PreSaveAction() function is our entry point to make any kind of validations. Put it in the Content Editor web part (or if you need to do some kind of Server side processing, put it in the SharePoint Web part)

I used it to get the value from hidden field (which was populated in the web part Page_Load method) and wrote the below function in the ascx file.

function PreSaveAction() {
var columnName = document.getElementById('<% =hdnMandatoryColumn.ClientID %>');
if (columnName != null && columnName.value != null) {
var column = getTagFromIdentifierAndTitle("input", "", columnName.value)

if (column.value == '') {
   alert(columnName.value + ' is a required field.');
   return false;
}
else {
   return true;
   }
 }
}

If you want to link the PreSaveAction function to a custom edit or new form, make sure that you call the PreSaveItem function as in default form and not PreSaveAction function directly.

Advertisements

About Rupesh

.NET and SharePoint 2010 developer
This entry was posted in Misc and tagged , , , , . Bookmark the permalink.

4 Responses to SharePoint 2010 Validation using Javascript

  1. Pingback: Pro Tip: Client Side Validation with Nintex Forms – Henry Chong

  2. image editor says:

    Hi, Neat post. There’s a problem with your website in internet explorer, would check this? IE nonetheless is the market chief and a large portion of folks will omit your wonderful writing due to this problem.

  3. Hello i am kavin, its my first occasion to commenting anyplace,
    when i read this post i thought i could also create comment due to this good
    post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s