Tallan's Technology Blog

Tallan's Top Technologists Share Their Thoughts on Today's Technology Challenges

How to add custom validation logic to validate a phone field in SharePoint

Reddy Kadasani

Tallan’s SharePoint Practice Lead, Reddy Kadasani, shows you how to add custom validation logic (UI portion, and code) to validate a phone number field.

How to add custom validation logic to validate a phone field (UI)

  1. On the UI, go to List Settings
  2. Click on Column that stores the phone (code below assumes the field is called Business Phone. Note that Business Phone is a built-in field in SharePoint)
  3. Expand the Validation section
  4. In the formula area enter the following code: format being checked is like (111) -111-1111
=AND(
LEN([Business  Phone])=14,
IF(ISERROR(FIND("(",  [Business Phone],1)),
FALSE,
(FIND("(", [Business  Phone]) = 1)
),
IF(ISERROR(FIND(")",  [Business Phone],5)),
FALSE,
(FIND(")", [Business  Phone], 5) = 5)
),
IF(ISERROR(FIND(" ",  [Business Phone],6)),
FALSE,
(FIND(" ", [Business  Phone], 6) = 6)
),
IF(ISERROR(FIND("-",  [Business Phone],10)),
FALSE,
(FIND("-", [Business  Phone], 10) = 10)
),
IF(ISERROR(1*CONCATENATE(MID([Business Phone], 2, 3), MID([Business Phone], 7,  3), MID([Business Phone], 11, 4))),
FALSE,
AND(
1*CONCATENATE(MID([Business Phone], 2, 3), MID([Business Phone], 7, 3),  MID([Business Phone], 11, 4)) > 1000000000,
1*MID([Business  Phone], 2, 3) <> 911,
1*MID([Business  Phone], 7, 3) <> 911,
1*MID([Business  Phone], 7, 3) <> 555
)
)
)
  1. In the custom message field enter the message you would like the user to see when there is a validation error
  2. Save and close

How to add custom validation logic to validate a phone field (Code)

To do this through code (Visual Studio), in the field definition file put code similar to the following code. Code below assumes the field is called Work Phone.

<Field
Group="Custom  Columns"
DisplayName="Work  Phone"
ID="{56D153DB-5882-492F-9495-87C58B6C7DE2}"
SourceID="http://schemas.microsoft.com/sharepoint/v3"
StaticName="WorkPhone"
Name="WorkPhone"
Type="Text"
Required="TRUE"
MaxLength="255"
Description="Work  Phone">
<Validation
Message="Please enter phone number in this  format: (###) ###-####">=AND(LEN([Work Phone])=14,IF(ISERROR(FIND("(",[Work  Phone],1)),FALSE,(FIND("(",[Work Phone])=1)),IF(ISERROR(FIND(")",[Work  Phone],5)),FALSE,(FIND(")",[Work Phone],5)=5)),IF(ISERROR(FIND(" ",[Work  Phone],6)),FALSE,(FIND(" ",[Work Phone],6)=6)),IF(ISERROR(FIND("-",[Work  Phone],10)),FALSE,(FIND("-",[Work  Phone],10)=10)),IF(ISERROR(1*CONCATENATE(MID([Work Phone],2,3),MID([Work  Phone],7,3),MID([Work Phone],11,4))),FALSE,AND(1*CONCATENATE(MID([Work  Phone],2,3),MID([Work Phone],7,3),MID([Work  Phone],11,4))&gt;1000000000,1*MID([Work  Phone],2,3)&lt;&gt;911,1*MID([Work  Phone],7,3)&lt;&gt;911,1*MID([Work  Phone],7,3)&lt;&gt;555)))
</Validation>
</Field>

Note: The validation formula should return a boolean, otherwise you will get an error when adding this column to a list. Also, the formula should reference the field using the display name of the column

using the format [Display Column Name]. If you don’t do so, you will get an error adding this column: “Formula references an unknown field”

Reference: http://thechriskent.com/2012/08/16/validate-email-address-columns-in-sharepoint/

No comments

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> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

\\\