Wednesday, September 4, 2013

SharePoint Custom Field - Regex Validator

Finally!!.. I have published my first project on codeplex. Here is a brief about that project:

Project Description
This is a SharePoint 2010 custom field type project that provides Regex (regular expression) validation on a text field.

In few words
This field provides some inbuilt validation options like Email, SSN, Phone no., IP Address and Alphanumeric. Besides this, it also provides an option to specify your own regular expression with an error message.

DecodingSharePoint_Regex_Field.jpg

When a user will try to add/edit list item, this custom field will check if the value matches the regular expression and it will throw an error, if not matched.

validation screenshot.jpg

How to add custom regex options
If you want to add some more inbuilt regex options like Email or SSN then you can make changes to the following files as described below :
  • In ControlTemplates\DS_Regex_FieldEditor.ascx
    • Add new item in "rbnRegexType" RadioButtonList control
      • e.g. <asp:ListItem Text="Alphanumeric XXX" Value="Alphanumeric"></asp:ListItem>
  • In DS.Regex.Common.cs
    • Create a new string and its value must be equal to the value specified in the previous step i.e. in DS_Regex_FieldEditor.ascx file
      • e.g. : public static string Alphanumeric = "Alphanumeric";
  • In DS.Regex.ValidationRule.cs
    • Add regex in StandardRegularExpressions Dictionary using the constant defined in DS.Regex.Common.cs file.
      • { DS_Regex_Common.RegexType.Alphanumeric, @"^\w+$"}
    • Add error message in StandardErrorMessage Dictionary using the constant defined in DS.Regex.Common.cs file.
      • { DS_Regex_Common.RegexType.Alphanumeric, "Please enter valid input"}

Installation
  • Add-SPSolution -LiteralPath <Path>\DecodingSharePoint_Regex_Field_Type.wsp
  • Install-SPSolution -Identity DecodingSharePoint_Regex_Field_Type.wsp -GACDeployment

You can download the source code and deployment package from here :
https://spregexfield.codeplex.com/

No comments:

Post a Comment