I recently stumbled over the blog post
Validation in SharePoint 2010. This blog post provides a good overview of what can be done with SharePoint built-in field validation. It also describes where the built-in validation falls short. Solutions with no custom code are bound to MS Excel style
formulas:
=[Budget]<1000
These formulas can cover basic validation rules. As soon as you need to do validation based on dynamic values (e.g. the current date) or pre-defined patterns (e.g. telephone numbers, social security Number, e-mail1, etc.) you need to create a
custom field definition, implement a List Item Event Receiver or do hacky stuff with client script validation.
All of you that already wrote tried to write complex validation formulas know what I'm talking about.
The MatchPoint Form Web Part provides a powerful field value validation mechanism using the
Expression Engine that enables you to write complex, no-code validation rules (including
regular expressions).
Validation Expression Examples
The MatchPoint Form Web Part lets you configure a validation expression for every writable form field. In the following examples I use two expression variables
FieldValue (the current field value) and Form.Values.XYZ. The latter will be introduced with MatchPoint 3.1.0 and provides access to other fields on the form.
Example #1
In a project management list the field budget must not exceed 1000$:
FieldValue <= 1000
Managers are allowed to set a budget exceeding this limit:
FieldValue <= 1000 || MPUtility.IsUserInRole("Manager")
Example #2
The end date of a task list item should not be in the past:
FieldValue > DateTime.Now
The end date of a task has to be at least two days from today:
FieldValue > DateTime.Today.AddDays(2)
It's OK for the end date to be in the past if the task is completed:
FieldValue > DateTime.Now || Form.Values.Status == "Completed"
Example #3
Validation expression for a phone number with format (541) 754-3010 or
754-3010 using a regular expression:
FieldValue.Matches("^(\\(\\d{3}\\)\\s?)?\\d{3}-\\d{4}$", "None").Count > 0
Filter on a set of invalid characters (for instance !?_-*):
FieldValue.Matches("^[^_./*<>]*$", "None").Count > 0
Setup
You need to set a Form Web Part as default edit form on a SharePoint list in order to validate form values with MatchPoint expressions. The following link and video provide information on how to use form web part as default edit form:
How to use the Form Web Part to edit a SharePoint List Item
In order to enable validation on a MatchPoint form field you have to add the
Validator element to the field configuration:
The Validator element provides three settings:
-
Expression:
Specifies an expression that is expected to return true for valid field values. Use
FieldValue to access the field's value.
-
Message:
Specifies the message that will be displayed if the field value is invalid. Use
{FieldValue} to access the fields's value.
-
ExceptionBehavior:
Specifies the behavior, if an exception occurs during the evaluation of the expression.
1:
I Knew How To Validate An Email Address Until I Read The RFC