Validation on SharePoint Forms – Part Three
In parts one and two on this topic, I gave an overview of SharePoint form validation and gave an example of how you can implement some validation with JavaScript. In this post, I’ll show how you can use more standard .NET validation with a custom form. (Be sure to check out Part 4 as well, which covers the PreSaveAction() function.)
Let’s use an example where you want to validate a US ZIP Code on your NewForm.aspx for a list. The first steps are to create your list with whatever columns you want , including the ZIP Code column as a Sinlge line of text; copy NewForm.aspx (never edit the original in case you need it!); remove the default List Form Web Part (LFWP); and add your own Data View Web Part (DVWP). Some folks out there will tell you that you should leave the default LFWP on the page and hide it to avoid problems; I think this is a holdover from SharePoint 2003 as I’ve never had any issues with removing it. I’ll gloss over the details on these first steps, as I cover them in other posts.
Now that you have the DVWP on the page, you want to focus on the ZIP Code column. Just a note: I always work in the Split View in SharePoint Designer so that I can watch what impact the changes I make using the dialogs have on the code within the page. Highlight the ZIP Code, right click and choose Show Common Control Tasks *or* click the twiddle next to the formfield control.
You’ll see that the control is bound to your ZIP Code column and that it is formatted as a List Form Field. Change this to a Text Box. This will change the line in your page’s code which looks something like this:
<SharePoint:FormField runat="server" id="ff4{$Pos}" ControlMode="New" FieldName="ZIP_x0020_Code" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Value','ValueChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@ZIP_x0020_Code')}" />
to something like this:
<asp:TextBox runat="server" id="ff4{$Pos}" __designer:bind="{ddwrt:DataBind('i',concat('ff4',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@ZIP_x0020_Code')}" />
So now you have a plain old asp:TextBox rather than a SharePoint:FormField, but the bindings are still the same. Now add a RegularExpressionValidator to the page by choosing Insert / ASP.NET Controls / More ASP.NET Controls and selecting the RegularExpressionValidator in the Validation section of the Toolbox.
If the Tag Properties pane isn’t open, then right click on the validator and choose Properties to open it. Now specify the required properties, something like the following:
-
ErrorMessage: Please enter a valid ZIP Code in the format 99999-9999
-
SetFocusOnError: True
-
Validate Expression: Since we’re validating a ZIP Code, SharePoint Designer already knows about how it ought to be structured. Simply click on the builder and choose U.S. ZIP code from the list, giving
\d{5}(-\d{4})?
-
ControlToValidate: This should be the ID of the ZIP Code Text Box control. In this example, it is ff4_new.
If the user enters something for the ZIP Code with doesn’t pass the validation test (5 digits, followed optionally by a dash and 4 digits), they will see the error message, and the focus will be on the Zip Code column.
how can you add validation to a lookup field.
Dave:
You’ll have to be a little more specific than that! However, the general answer is to do it with .NET code server side or script client-side.
Check Part 4 of this series for an example with script.
M.
Hi ,
Can any body suggest me that how can we validate text field ,that it takes only characters not numbers.
thanks in advance
Rishi:
There are a couple of ways you can do this. You can create a custom field type that does the validation or you could use script in the PreSaveAction() to validate with a regex.
M.
Hi Marc,
Thanks for this article, this was very helpful.
But I have a problem impleting this.I have a list with nearly 40 columns to fill . I have created the columns through the sharepoint UI “Create Column”.
So when I try to edit the page with SP designer , I cannot hide the List form webpart , neither cannot add the regular expression validator .
If I create a new list , i can hide the LFWP and add the DFWP and have the regular expression validator. But with that the Attachment field appears and removing the attachment field is causing an error.Moreover I dont want to add the 40 colums again throgh this new way.
Could you please help me to have the vadiation done on few fields of the form (created through the “Create Colum”of the SP UI)
Thanks,
Jhu
Jhu:
You might want to look at the other posts in the series, specifically this one. You can do the validation entirely with script if you don’t want to customize the form.
M.
Hi Marc ,
I have tried to use the regular expression validator to validate a field to have only 7 digit numeric . The regular expression i used is \d{7}. The validation throws error message even if i enter the 7 digit. But the email works fine.
Could you please point out , where it might be wrong. The regular expression is correct , because elsewhere it is validating . More over all the regular expression with “d” is failing.
Thanks,
Jhu
What about validating the date fields? That’s what I am trying to do? Make sure end date > start date? Thanks in advance
Jason:
To validate columns against each other, the approach in Part 4 using PreSaveAction() is usually best.
M.
Thanks a lot for very useful post
hi,
this is my control code
next, in controltovalidate if I set it to “ff32_new” it is throwing error. Can you please guide me
raghu:
I think you posted some code which didn’t come through (WordPress comments only allow certain markup). Try posting the details to one of the public forms out there and ping back a link if you are still stuck.
M.