Click here to Skip to main content
Click here to Skip to main content

Adding Validation to a Custom List Control in Sharepoint

By , 28 Jul 2008
 

Introduction

Recently, one of the requirements was to put validation to a custom list control such as Email field validation.

This validation can be achieved from inside the Sharepoint designer.

Following is a step by step walkthrough to validate a list field.

Background

You should know how to create a Sharepoint list and how to open the site in Sharepoint designer.

  1. Following is a list with Email fields. Let's say we have to validate so that only a valid email can be entered.

    If not valid, then the Error Message appears next to the field:

    072908_0945_Addingvalid1.png

  2. Now open the site in the Sharepoint designer and then expand the list and open the Newform.aspx of the list to be validated:

    072908_0945_Addingvalid2.png

  3. As you can see in the image above, the Newform.aspx of Validation list is being opened in the Sharepoint designer.

  4. Now right click on this Validation list Web part in Sharepoint designer and choose Web Part properties:

    072908_0945_Addingvalid3.png

  5. The properties dialog box will open for the Validation list here. Choose Hidden under layout and say OK.

    (This web part can be deleted as well, but due to some known problems because of deletion, it is best to hide this).

    072908_0945_Addingvalid4.png

  6. Now click exactly underneath the hidden list web part and go to Insert->Sharepoint Controls-> Custom List Form… and the Wizard will ask to choose a List or Document Library form based on existing list.
    Choose your Validation list and select New item form under type of form to create option and hit Ok.

    072908_0945_Addingvalid5.png

  7. You will get the Validation List underneath the hidden list as shown as selected image below:

    072908_0945_Addingvalid6.png

  8. Now from here, we can customize all the controls based on requirement. As we are customizing the E-Mail field, right click on the email field box and choose Show common control tasks from the context menu:

    072908_0945_Addingvalid7.png

  9. This will give you Common Formfield tasks option as shown below next to the Email field. Here we can change the format of the data field selected (default is List form field). As we need Email field to be textbox where validation can be applied, Choose Textbox from Format as dropdown instead of List form field.

    072908_0945_Addingvalid8.png

  10. After the above step, List will look like this (custom formatted):

    072908_0945_Addingvalid9.png

  11. Now drop a RegularExpressionValidator control from the Validation option under ASP.NET controls from the toolbar as shown below:

    072908_0945_Addingvalid10.png

  12. Now specify the required properties (such as ControlToValidate, ErrorMessage, SetFocusOnError to true, Validate Expression) of the RegularExpressionValidator from the Tag Properties toolbar. For ControlToValidate, choose the ID of the Email field.

    After selecting it and getting the ID from the Tag Properties toolbar as you do in Visual Studio, see below the set attributes in blue for the Email field:

    072908_0945_Addingvalid11.png

  13. Now, save the changes and go back to the list in Internet Explorer and try to type in an invalid email id and you will get the following:

    072908_0945_Addingvalid12.png

Happy Sharepointing…

History

  • 29th July, 2008: Initial post 

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Rajan Bhanot
Software Developer (Senior) Capgemini
India India
Member
Rajan Thakur is a Senior MS Solutions Consultant, working with SharePoint 2007 and .net for over 6 years now.
 
Email: rajan_thakur78@yahoo.com

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionRequiredField validator produces an unexpected error has occured error...memberrdkll2k8 Nov '11 - 9:49 
When I attempt to add the requiredfield control to a SharePoint 2007 custom form connected to a list attempting to require a selection in a lookup field populated drop down list I receive the "an unexpected error has occured" error. Unfortunately there are no further details provided.
 
I'm guessing that it has something to do with the controltovalidate value but I've tried the SharePoint:FormField id of ff1{$Pos} and a number of other variations with no success. Maybe I'm missing something else altogether.
 
I know that the form works without the validator because as soon as I delete it from the page it loads just fine.
 
Any ideas?
 
Ralph
Questionhow to validate DropDownMenu?memberAbdullah_m27 Jun '10 - 21:48 
what if i need to put "Required Validation" on DromDownMenu?
 
i can't convert it to Textbox because it'll lose the values inside it.
QuestionHow to do "Required field" validation like a SharePoint:FormField?membermatturn16 Jul '09 - 14:56 
I've got something very similar to this article working. However, when I apply a RegularExpressionValidator to an asp:TextBox to check if it's null or not, it doesn't validate when the box is empty. I've been using variations on "^.+$" to do the matching.
 
Even if it did match however, it would only validate on loss of focus on the TextBox, not on committing the form like a SharePoint:FormField does.
 
How could this be done?
GeneralValidation not working for US Phone numbermemberMember 16625819 Apr '09 - 19:23 
Hi Rajan,
 
First of all thanks for this article. It really is very helpful to a person who does not know much about MOSS 2007 Customization.
 
I am trying to repeat the same for validating us phone number it does not seem to work but works fine if i test it for a email id it works fine. shoud i change soething when i do it for a us phone number.
 
Thanks,
 
Hemanth

GeneralRe: Validation not working for US Phone numbermemberbdimitre9 Feb '10 - 4:33 
Hello,
 
I get the same problem. Works great for the e-mail validation but then when i add a phone validation or zip code validation it does not work. The e-mail validation continues to work but on the other two no matter what format i try to enter the data in it does not accept it.
 
is this a glitch with SharePoint? Can it not accept more than one validation?
 
Does any one know the solution to this?
GeneralProblemmemberredmondscorpio27 Jan '09 - 8:06 
I pretty much followed what you have here with a small problem of, say you have 2 fields, 1 is a Sharepoint:FormField and is marked required. The other is a converted formfield which is now a textbox with a validator. If you populate the converted textbox field but not the sharepoint required field and submit, after postback, the data is gone in the textbox field. Did you experience this issue?
 
Thanks
QuestionHow about cross validationmemberSingh_h19 Jan '09 - 9:24 
I am new to sharepoint development, especially haven't done much with designer. I need to add cross field validation on new form. Field 1 is a drop down list with Value 1,2 amd 3. If Field 1 = 2 then
user must fill the next field - Field 2.
Do you have any idea on how to accomplish this?
 
Thanks,
Singh
Questionhow to do achieve same objective using C# codememberMember 421240726 Nov '08 - 22:48 
I have created custom e-mail field using sharepoint feature, i have to validate the e-mail field using c# code. can you help me, its urgent.
QuestionDoes this method work on Edit form also?membersandeep Kumtakar3 Nov '08 - 21:48 
I have more than one site collection on a single port. Now when i implement this method on Edit form it redirects itself to main web application. Can this be resolved
 
"Success is 1% Inspiration and 99% Perspiration"

QuestionControl 'ff11_1' referenced by the ControlToValidate property of 'RequiredFieldValidator1' cannot be validatedmemberBrianVen3 Nov '08 - 2:05 
I get the error:
 
Control 'ff11_1' referenced by the ControlToValidate property of 'RequiredFieldValidator1' cannot be validated
 
'ff11_1' is the ID of the control to be validated, but it seems to be wrong.
 
The control lives in a content page that lives in a master page.
 
Any idea what the ID should be when working with master pages ?.
 
I have tried all possible combinations but without success.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Permalink | Advertise | Privacy | Mobile
Web03 | 2.6.130523.1 | Last Updated 29 Jul 2008
Article Copyright 2008 by Rajan Bhanot
Everything else Copyright © CodeProject, 1999-2013
Terms of Use
Layout: fixed | fluid