Home > CMS- Content Management System > Sharepoint > Custom validation on default list form in SharePoint 2013

Custom validation on default list form in SharePoint 2013

In SharePoint, there is requirement where we need to apply custom validation based on certain condition on specific column value. Validation may include show hide field, make required field, show error massage on blank form submission etc.

In this article I will explain a scenario involving such validations and some functions for validations using script.

Let us assume our requirement has a list called ‘Travel’ to getting travel related data like status code depending on mode of transport. The list has the following fields:


Here we apply custom validation based on ‘Travel Mode’. ‘Travel Mode’. is choice field which has values “Train, Plane and Others”

Below are condition we need to add in our custom list form for the validation

  • If TravelMode==’Train’ then show ‘PNRCode’ with mandatory
  • If TravelMode==’Plane’ then show ‘ANRCode’ with mandatory
  • If TravelMode==’Other‘ then show ‘StatusCode’ with mandatory

Note: When you create a list, Please note that following columns should not be mandatory in list.
* PNRCode
* ANRCode
* StatusCode

Now, we need to edit list form and add a jQuery reference after.

<asp:Content ContentPlaceHolderId=”PlaceHolderMain” runat=”server”>

Add references to the jQuery libraries and a custom JavaScript file that we create as follows:

Please notice that initial we will hide these above three columns and call ‘OnTravelChangeNewForm’ function defined in PreSaveAction.js file.

Check out ‘OnTravelChangeNewForm’ function here based on drop down value we are calling FieldMakeHidden(), FieldMakeVisible() and FieldMakeMandatory() functions.

FieldMakeHidden() function: Attempts to make fields hidden

SpecificFieldTitle() function : Attempts to return a specific h3 Title of Field Name

FieldMakeVisible() function: Attempts to make fields visible

FieldMakeMandatory() function : Parameter like FieldName and boolean value true or false to make fields are mandatory.

Basically, even though the columns are not specified as “Required” in the list definition, we are trying to make the display that it is mandatory for the users to fill in a value for this column.

ValidateNewForm() function : Does actual validation in form on submit button click

FieldIsHiddenOrNot() function: Check FieldName it visible or not and then validate it.

Open the new form of the Travel List

When Train is selected, observe that PNRCode columns are displayed as mandatory and other two column are hidden.

On blank submit data were we show custom error message

Reference:
Custom Validation

This Article is TAGGED in , , , . BOOKMARK THE permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">