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.

image

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.

New Picture (3)

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.

image

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.

image

Similar Posts

28 Comments

    1. 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.

  1. Hi ,

    Can any body suggest me that how can we validate text field ,that it takes only characters not numbers.

    thanks in advance

    1. 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.

  2. 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

    1. 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.

  3. 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

    1. 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.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.