Skip to main content

Share point Validation



The Microsoft.SharePoint.WebControls namespace of the  Microsoft.SharePoint.dll contains a number of validation controls that can be used on application pages and web parts to validate user entry in the SharePoint controls.
The different validation controls are:
  • InputFormRequiredFieldValidator
  • InputFormRangeValidator
  • InputFormCompareValidator
  • InputFormRegularExpressionValidator
  • InputFormCheckBoxListValidator
  • InputFormCustomValidator
If you want to use these SharePoint control validators, you have to add a page directive to the page:
<%@ Register TagPrefix="spuc" Namespace="Microsoft.SharePoint.WebControls"
             Assembly="Microsoft.SharePoint, Version=12.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
The following sections describe the use of the different SharePoint validation controls.

The InputFormRequiredFieldValidator control

This control inherits from the standard ASP.NET RequiredFieldValidator and has the same functionality.
inputformrequiredfieldvalidator

Properties inherited from the standard ASP.NET RequiredFieldValidator control: 
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.
Important properties of the SharePoint InputFormRequiredFieldValidator control:
  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Satic. 
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

The InputFormRangeValidator control

This control inherits from the standard ASP.NET RangeValidator control and has the same functionality.
inputformrangevalidator

Properties inherited from the standard ASP.NET RangeValidator control: 
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • Type: indicates the type of data to validate. If this property is omitted, the default is String.
  • MinimumValue: the lower boundary. The minimum value itself is considered as a valid entry.
  • MaximumValue: the upper boundary. The maximum value itself is considered as a valid entry.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.
Important properties of the SharePoint InputFormRangeValidator control:
  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Satic. 
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

The InputFormCompareValidator control

This control inherits from the standard ASP.NET CompareValidator control and has the same functionality.
inputformcomparevalidator




Properties inherited from the standard ASP.NET CompareValidator control: 
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • ControlToCompare: This property is required and must contain the id of the control against which the first control must be validated.
  • Type: indicates the type of data to validate. If this property is omitted, the default is String.
  • Operator:  This property specifies the comparison operation. Possible values are Equal, NotEqual, GreaterThan, GreaterThanEqual, LessThan, LessThanEqual, DataTypeCheck.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.
Important properties of the SharePoint InputFormCompareValidator control:
  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Satic. 
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.
It is slightly different if you want to validate a date entered in a SharePoint DateTimeControl. This control is a composite control and you have to validate the date entered in the Date control:
 
I got the milk from Greg Galipeau’s blog in his post about validating an DateTimeControl with a standard ASP.NET Compare validator.

The InputFormRegularExpressionValidator control

This validator control can be used to validate user input against a regular expression. It inherits from the standard ASP.NET RegularExpressionValidator. Following example checks wether the user entered a valid email address.
inputformregexvalidator

Properties inherited from the ASP.NET RegularExpressionValidator control:
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • ValidationExpression: contains the regular expression against which the user entry must be validated.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.
Important properties of the SharePoint InputFormRegularExpressionValidator control:
  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Satic. 
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

The InputFormCheckBoxListValidator control

This validator control can only be used against an InputFormCheckBoxList control (and not against an InputFormCheckBox control). It works as a required field validator: if no options are selected when clicking the OK button, the specified error message will appear:
inputformcheckboxvalidator

      
      
      
      

If you try to use it in combination with a InputFormCheckBox  control, you will get an unclear error message (not informing you about the real problem) but when looking with Reflector into the code, it learns you that the control referenced in the ControlToValidate property is casted to an InputFormCheckBoxList  control.

The InputFormCustomValidator control

If one of the previous validation controls does not suit your needs, you can define a custom validation function. You have the choice to define a client-side validation function or a server-side validation function, or combine both. Best practice is to perform server-side validation, even if you use a client-side check because some smartasses can try to bypass your client-side script.
 inputformcustomvalidator-serverside
This is an example of server-side validation:

In your code-behind you define the controls as protected class-level variables:
protected InputFormTextBox UsernameTextBox;
protected InputFormCustomValidator UsernameCustomValidator;
And you add the server-side validation method. This method accepts 2 incoming arguments:  a sourceobject being the control to validate, and a args object having properties as Value and IsValid. The Valueproperty contains the value to validate. Set the IsValid property to true if the validation is successful or set the IsValid property to false if the validation fails.
protected void ValidateUserName(object source, ServerValidateEventArgs args)
{
    if (args.Value.Length >= 10)
        args.IsValid = true;
    else
        args.IsValid = false;
}
Properties inherited from the ASP.NET CustomValidator control:
  • ControlToValidate: This property is required and must contain the id of the control to which this validator control applies.
  • ClientValidationFunction: contains the name of the javascript function that needs to be executed.
  • OnServerValidate: contains the name of the server-side function that needs to be executed.
  • ErrorMessage: This property contains the error message that will be displayed when the validation fails.
  • SetFocusOnError: When this property is set to true, the focus is set on the control that caused the validation to fail.
  • EnableClientScript: Setting this property to false will bypass client-side validation.
A lot of people combine a custom validator control with a RequiredFieldValidator but this is not necessary because the ASP.NET CustomValidator control (and thus also the InputFormCustomValidatorcontrol) has a ValidateEmptyText property which can be set to true or false. If set to false, a blank entry is validated as a valid entry. If set to true, a blank entry will be considered as erroneous, thus excluding the need for a RequiredFieldValidator control.
If you want to use client-side validation, you have to set the ClientValidationFunction property. The value must be the name of a javascript function.
inputformcustomvalidator-clientside

The javascript function must reside in the  placeholder with ContentPlaceHolderIDPlaceHolderMain (where most of your controls reside). The function needs 2 arguments: a source object, being the control that needs validation; and an arguments object having properties like Value andIsValid. If you want the validation to succeed, you have to set args.IsValid to true; if you want the validation to fail, you have to set args.IsValid to false.

    
    
Important properties of the SharePoint InputFormCustomValidator control:
  • Display: This property influences the postion of the subsequent controls. If set to Static, the necessary space for the error message will be foreseen. Possible values are None, Dynamic and Satic. 
  • ErrorImageUrl: If this property is filled out, the image will be displayed together with the error message in case a validation error occurs.
  • BreakAfter: When this property is set to true, a linke break is inserted after the error message.
  • BreakBefore: When this property is set to true, a line break is inserted before the error message.

ValidationSummary control

You can combine any SharePoint validation control with the ASP.NET ValidationSummary control. There is no counterpart for it in SharePoint.
When you use no ValidationSummary control, all error messages appear next to or right under the control that fails validation. A ValidationSummary control presents the user with a lsit of validation errors at the bottom of the page.
validationsummary
You add a ValidationSummary control to your application page or web part as follows:
The different propeties on the ValidationSummary control are:
  • DisplayMode: this property defines the appearance of the summary. Possible values are List, BulletList and SingleParagraph.
  • HeaderText: set this property if you want to display a header text at the top of the summary.
  • ShowSummary: set this property to show the error summary on the page. In that case theShowMessageBox property must be set to false.
  • ShowMessageBox: this property indicates whether the validation summary is displayed in a message box or on the page. In that case also the EnableClientScript property should be set to true.
Normally you should also be able to set the Text property of the SharePoint validation controls to f.e. * to display an asterisk next to the control that failed validation but after a look inside theMicrosoft.SharePoint.dll with Reflector it show that the Text property is overridden and looses its original meaning.

Popular posts from this blog

How to deal with SharePoint 2010 exception "An exception occurred when trying to issue security token: The server was unable to process the request due to an internal error"

Scenario: You receive the below exception when you try to logon to a site that has been configured to use Claims Based Authentication with a custom membership provider using FBA credentials: Event ID from Event Log  - 8306 An exception occurred when trying to issue security token: The server was unable to process the request due to an internal error.  For more information about the error, either turn on IncludeExceptionDetailInFaults (either from ServiceBehaviorAttribute or from the <serviceDebug> configuration behavior) on the server in order to send the exception information back to the client, or turn on tracing as per the Microsoft .NET Framework 3.0 SDK documentation and inspect the server trace logs.. Explanation: This error started to appear in our QA environment which does not have Visual Studio installed. I have tried starting the service "Claims to Windows Token Service" but that did not help either. I have made sure that all config...

Custom Form Base Login Web Part With Sharepoint 2010

In my  Last Post , I talked about creating a Form based authentication web application with Active Directory LDAP. After implementing that you have surely noticed that, when you try to sign in after creating a site collection from FBA web application, you get the following sign in page: Surely, it does not look pretty at all. So, you might want to change this default login page. In order to do this, first let us create an empty SharePoint project in Visual Studio 2010. After that, add an application page by right click-selecting “Add New Item” and you will get like the following window in Solution Explorer: Notice that, when you add any application page to your project, Visual Studio automatically creates a Layouts  folder. To keep things simple enough for your understanding, you may keep your application page under a folder where you might name it as same as your project like the figure above. Now, add a reference for  Microsoft.SharePoint.IdentityModel...

Migrating from Skype for Business to Microsoft Teams: A Step-by-Step Guide

Do you still use Skype for Business to meet the communication and collaboration needs of your business? If so, now is the perfect time to think about switching to Microsoft Teams, a cutting-edge platform with cutting-edge capabilities and seamless connectivity with other Microsoft services. But if you're unfamiliar with the procedure, switching to a new platform can seem like a difficult task. I'll walk you through the process of switching from Skype for Business to Microsoft Teams in this article. Plan the migration in Step 1 You must make a plan before you start the relocation procedure. Set a deadline, make a list of all the Skype for Business capabilities you presently use, and choose whether to migrate gradually or all at once. Step 2: Set up your surroundings. Make sure your network and infrastructure fulfil Microsoft Teams' standards. Upgrades to your hardware, software, and licences might be necessary. Additionally, confirm that you have the right permissions to ca...