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. hey,

    i tried to implement this on one of my projects.. in the edit view, the asp:textbox does not have any value when it’s supposed to.. any ideas?

    1. Jason:

      I just tested this on the same list I used for my example above, and I see the value in the ZIP Code column on the EditFormCustom.aspx page which I created. Is your binding intact? My TextBox line looks like this:

      <asp:TextBox runat="server" id="ff4{$Pos}" text="{@ZIP_x0020_Code}" __designer:bind="{ddwrt:DataBind('u',concat('ff4',$Pos),'Text','TextChanged','ID',ddwrt:EscapeDelims(string(@ID)),'@ZIP_x0020_Code')}" />

      Are you missing the text=”{@ZIP_x0020_Code}” tag?

      M.

  2. Christian:

    I have a previous post which talks about filling SharePoint form columns with data passed in on the Query String.

    However, your question about populating a column with today’s date is probably worth a post of its own. I’ll get to it today!

    M.

  3. Cool, superintresting stuff there also in your other post, can’t wait to lab with your JS. If you have a solution from populating Today, that’s really useful!
    Keep on, Christian

  4. Hi Marc,
    Thanks for your posts.I tried all the validations on my list columns I copied this code in to CEWP in newform and editform .aspx pages instead of creating a new newform page/altering the default aspx page everything is working fine.But I got a list with columns like ‘Status’,’Closing Date’,’Create a New Stage?’ columns along with some other columns.My problem is if the Status of a task(list item) is ‘open’ and they entered a closing date then it should pop-up an error/alert like “If a task Status is open then there will be no closing date” and 2nd case is also similar to this if the task is closed and there is a closing date then they cannot check the ‘Create a new Stage?’ check box.
    I hope you got my question. Can you please help me how such data validations can be done?

  5. Marc:

    I just wanted to let you know that I have had very little luck finding any relevant, well-described SharePoint code articles before I stumbled upon yours. Please keep up the good work so that we can all benefit from your knowledge!

    Thanks a ton!

  6. Hello Marc,

    I’ve tried looking everywhere on the net trying to find a way to code a validation expression for the following:

    I need to have a field that will except 6 numbers followed by a period and must contain at least one number after the period. With an option of having up to three leading numbers. Here’s an example:

    900001.1
    900001.12
    900001.13

    I greatly appreciate any help you can give,
    Thank you,

  7. In the case of Server Side Validation after a postback, the ddwp doesn’t save the fields since it doesn’t fire the “TextChanged” event. Any way around this?

    1. Since you’ve replaced all of the default controls with asp controls and validation code, it’s sort of on you to make it work.

      I would usually leave the default controls and do the validation with script in the PreSaveAction function.

      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.