Phone Number Validation in SharePoint 2007

Yes, you can do ANY kind of validation such as phone numbers, email addresses, social security numbers, zip codes, whatever.  If you’re really nerdy, you can even validate IP addresses.  I just thought I’d catch your attention with the title, since phone numbers are one of the most frequent types of data that people need to validate.  This is all done using regular expressions, with NO programming involved!

What is validation?  It means that when you have a form with a box for someone to type their telephone number, you want to make sure they type an actual full phone number, in the right format.

Currently in SharePoint 2007, you can make a field required, and you can set it to a certain type of data such as a number or date.  On the other hand, in SharePoint 2010, validation is now a built in option each time you create a new column in any list or library.

Guess what… this solution entails creating a data view web part!

In this example, I have 3 fields I want to validate:

  • Email Address
  • Business Phone
  • Social Security Number.

In a previous video, I showed you how to Customize Form Pages in SharePoint (Video), so this is the prerequisite for doing the validation.  Once you’ve created a contact list, added a social security number text column, and opened the site in SharePoint designer and created the custom form, these are the steps you take to add the validation.  This will need to be done on the NewForm AND the EditForm of your list.

  1. In your custom form, click to select the Email Address field, and click the little chevron at the top right corner of it, like this:
    chevron
  2. In the Format As drop-down box, click to change the format to “Text Box”.
  3. Click to select the text box, right-click it and click Properties, so that the Tag Properties toolpane will show on the left side of the screen.
  4. Scroll to the very bottom of the tag properties pane to find the ID tag, and rename it to EmailTextBox, like this: Correction!  Don’t rename the text box.  You’ll just have to remember which one is which.
    image
  5. Click the Insert menu at the top of the screen, select SharePoint Controls, and click on More SharePoint Controls.  This will put a new toolpane on the right side of the screen, called Toolbox.  Click to expand the ASP.NET Controls, and the Validation section in there:
    image
  6. In the form, put the cursor next to the email address text box, on the right of it.  Double click the control called the RegularExpressionValidator to add it to the page.  Then, double-click the RequiredFieldValidator to add it also.I know what you’re thinking.  If I’ve already set it up as a required field when I created the column in SharePoint, why would I need to make it required again?  When you change a field to a text box in the form, it doesn’t recognize the SharePoint required field info at all.
  7. Click to select the red RegularExpressionValidator, and then take a look at that Tag Properties tool pane on the left again.  In the Behavior section, in ControlToValidate, select the EmailTextBox default name of that text box, which will be something like ff20.
  8. Do the same thing with the RequiredFieldValidator.  Set its ControlToValidate as the EmailTextBox default name of the text box.
    You only need to add the RequiredFieldValidator if the field needs to be required.  Otherwise you can leave it off.
  9. Select the RegularExpressionValidator again, because now it’s time to set up the regular expression.  In the Tag Properties pane’s Appearance section, set the Error Message to say:
    Not a valid email address
  10. In the Behavior section of the Tag Properties, go to the ValidationExpression box, and click the ellipsis button to see this, and select “Internet e-mail address” and click OK:
    imageHere’s what the Tag Properties pane looks like when the Regular Expression Validator is selected:
    imageTHIS IS SO COOL.  I know you want to, go ahead and try this one out, so ahead and save the page and hit F12 to preview in the browser.  Try typing incorrect and correct email addresses, and you’ll see that when it’s not a real email address, the red error comes up.
  11. Time do set up the validation for the other two fields.  Repeat steps 1 through 10 for the social security number and phone number, but KEEP READING BECAUSE THERE’S A TRICK TO THESE OTHER TWO.
  12. For the ControlToValidate box for each control, be sure to select the appropriate one (of the above three).  The ErrorMessage obviously needs to be different for each field also.

SO HERE’S THE TRICKY PART THAT FRUSTRATED THE HELL OUT OF ME.  In the regular expression editor, when you select the “U.S. Social Security number” or the “U.S. Phone Number” patterns, they (and others) JUST DON’T WORK, so here’s how to fix them…

First of all, this is the pattern for a social security number:
image

It’s 3 digits, then a hyphen, then two, a hyphen, then four, like 765-76-9900.  See in the pattern in the box above, the numbers with the curly brackets {} around them represent the number of numbers in a row.  The problem is that SHAREPOINT DOESN’T LIKE THE CURLY BRACKETS.  So, any time you see a pattern that has curly brackets in it, you have to modify it.  So the above pattern for a SSN would need to be modified to be:
ddd-dd-dddd

Same goes for the phone number.  Here’s what the modified phone number expression looks like:
image

This is the finished version of the NewForm.  Again, you’ll need to do the exact same thing on the EditForm.  I’ve typed a couple of them incorrectly, so each shows me an error message and the form can’t be submitted until they’re correct.

image

Here’s my associated video, showing this process: Validate Email Addresses and Phone Numbers in SharePoint 2007 (Video)

In all my frustration figuring this out, here are the good reference sites that I came across, regarding regular expression patterns, in case you want to create your own:

Regular-Expressions.info

How To: Use Regular Expressions to Constrain Input in ASP.NET

Regular Expression Library

34 comments

  • I added the regular expressions and it works great. However, I have noticed when using a workflow those fields do not carry over. Is there a work around for this.

    Like

  • This will unghost a page though, right? How does this affect performance?

    Like

  • Rebecca, Not sure what you’re doing with your workflow, or what you mean by carry over. The validation only exists on the newform and editform pages that you add it to, it’s not inherently part of the column itself. Dan, Yes, it unghosts the page. As mentioned in my first screencast, only create custom forms if absolutely necessary. It will affect performance the same way as unghosting anything else, I guess. Not sure how to answer that.

    Like

  • nahdarp@gmail.com

    I followed the article and was able to make the validation work. But when I tried submitting the form, the fields that used custom validation did not get posted to the list. When converting the List Form Field to Text Box, does it lose connectivity with the SharePoint list?

    Like

  • Thanks nahdarp! It was my mistake, I shouldn’t have renamed the text boxes. I’ve inserted the appropriate corrections into the blog post.

    Like

  • cylan97@gmail.com

    can regular expressions to be used on “Column Validation” on Sharepoint Foundation? I tried to put regular expressions under “Column validation” on the “Column Change” page and it came back with an error

    Like

  • spfsmc@gmail.com

    How do you customize an Edit form with validation on Sharepoint Designer 2010? Apparently choosing “Text Field” was not an option for an “Edit Form.”

    Like

  • regarding that last question: You’re right, it doesn’t convert to a text field. Not sure why they would have removed that functionality. Here’s what to do: Delete the default form field. From the toolbox, in the data view controls section, insert a text box from there. Then, click the chevron next to the next box, and change the data field to your original field name.

    Like

  • siddharthvaid@gmail.com

    hey! i think this is just great and very well explained. we just moved to sharepoint 2010 foundation and hope the same works there. although i do want to know that if we SYNC the list with outlook then DOES Sharepoint enforce the validation. ie, if an entry in made in outlook offline, Sharepoint will not accept patterns that dont match the validation, when the user tries to SYNC the list.

    Like

  • SharePoint 2010 has validation built in, so you wouldn’t need to do this. Each time you create a new column, there’s a validation section on that screen.

    Like

  • This is really useful. Could you also do a similar walkthrough for the ‘compare’ validator? This would be really handy to stop duplicate entries. Based on your explanation above I can nearly figure it out – but just not sure how to tell it what to compare against.

    Like

  • Laura, thank you for the article. In your comment @7/25 you note that 2010 provides for a column validation formula, but I can’t find any reference to their syntax. Do you know if regular-expressions are supported and if so, the correct syntax?

    Like

  • Hi Garth, Here are a couple of blogs people have written, but you’re right that there’s not much out there yet. http://tinyurl.com/2amy4s7 or http://tinyurl.com/2etvjzp

    Like

  • somitra.gupta@gmail.com

    i m getting an unexpected error ocurred message evry time i put any asp.net validation on the custom list form and see the privew in browser in spd.please help

    Like

  • qun_yue@yahoo.com

    Great article, so we can validate field without installing extra web part on moss 2007. That’s wonderful. I have a question about the required fields on DVWP. I found out recently, when insert a DVWP on a web page, the “required field” rule don’t work for Choice type fields, such as dropdow or radio button, even you have that field set as required in list property. Other type of fields, the “required field” works fine. Do you know if this is a bug in DVWP? Any solution to enfore the required field in DVWP? Thanks. Qun

    Like

  • shankkarsankar@yahoo.com

    I do not have SharePoint Designer, but have the out of box functionality to build Forms and Workflows. My issue is…how do I check if the values entered in a free form text field is an email address and if it is not in a valid format, delete the value so that the SP email notification function works as expected and workflow does not error.

    Like

  • New Distribution List

    Is it possible to take a Distribution List from Outlook and place in to SharePoint 2007? I have a staff member who would like to have his mail distribution list available to his co-workers to use. We work in a military hospital and every week he has to send e-mails to different units on the base a list of ‘No-shows” of appointments. Thanks and hope you can help, Frank

    Like

  • so it’s not an actual Exchange distribution list, but just one that a person has in their Outlook contacts list? No, that capability doesn’t exist in a sharepoint contact list. Options: 1. Get the email admin to create a real distribution list in the main directory, and that list could have its own email address that people could send to. 2. Put all the individuals in a sharepoint group, and there’s an ability in the group to put check boxes next to the people’s names and “send email”.

    Like

  • Hi Laura, How about validating the radio button controls? i have a choice field with two options (radio buttons) and i dont want the default selection. I did make the field required, but when i am using dataform webpart, required field validator is not working for this choice field. whereas it is working for the textbox. Thanks in advance.

    Like

  • Laura, I think I love you. Thank you.

    Like

  • sharon.aguinaldo@edd.ca.gov

    Is there a way to use regular expression to validate an SSN field in SP 2010 using column validation only? I tried google and cannot find a simple solution. Thank you, Azu

    Like

  • kmhaga03@gmail.com

    I left the textbox as the default name as stated in the instructions and set up the validation on that name. Yet, it still doesn’t update it in the list item. The field is blank. Same as what nahdarp@gmail.com said. Code:

    Like

  • kmhaga03@gmail.com

    I’m having the same problem nahdarp@gmail.com had. Even though I’ve followed the instructions (obviously doing something wrong still) and left the textbox name the default, it’s still not mapping over to the list.

    Like

  • HK, This is not something that needs to be done in code. When you’re creating the column in the list, just in the browser, on the new column screen, type all the choices in the box of choices, and then scroll down and clear out the “default value” box and then click OK. Sharon, I don’t know, I’ve never tried the column validation feature in 2010, it’s not very well documented, is it. Nahdarp and kmhaga03, Try instead of converting the box at step 2, just delete the list form field and insert a new text box from the list of data view controls in the toolbox. Then be sure and map to the correct data field before moving on to step 3.

    Like

  • Laura, thanks for the post! I found a kind of a Sharepoint bug… I have 2 required fields in my form, the first is a Sharepoint native FormField and the other is a asp:textbox with a required field validation rule. If I submit the form when the asp:textbox field is populated, but leaving the sharepoint formfield blank, the native required field red message appears. After populating the sharepoint form field and submiting the form again, nothing is recorded in the textbox field. When the form is submitted for the first time, if all required fields are populated, the data is properly recorded. Did you experience this issue? I really need to fix this!

    Like

  • Hi, Laura, This is a very good post, I learnt the other things as well. My problem is that, I followed your post to validate a US telephone number(using default phone regex) but my validation never lets me pass, no matter what I input. It is always wrong with my input. Have you ever experience this situation? I even tested my regex in ASP.Net (Visual Studio) and it worked very well. Please, give me some hints! Ella

    Like

  • Fabiano, Sorry, I’ve never seen that happen. Ella, I struggled a bit with the regex syntax also, but all of my trials and tribulations are documented in this blog post, and I didn’t have any further problems.

    Like

  • rstims@yahoo.com

    I have a solution to your curly bracket problem. That is, (a) create a custom variable within your form and add your RegEx pattern to a literal text and (b) call the variable in the ValidatorExpression attribute of your validation tag. For example, to validate 3 ‘a’ characters in my test field, I did the following: In ‘dvt_1.rowedit’ template (of my custom edit form), I added the following custom variable: a{3} In validation tag associated my test field, I added the following to my validation tag for said field: ValidationExpression=”{$abbrRegExPattern}” As a result, I was allowed to submit more less and no more than three (3) ‘a’ characters in the field.

    Like

  • I have a solution your to curly bracket problem. Create a variable (xsl:variable name=”regExPattern”) within the main date template, and add the Regular Expression pattern within text tags (xsl:text) of said variable. Then, in the RegEx validator, add the variables names within curly brackets (ValidationExpression=”{$regExPattern}”). In my test, I created a variable containing a{3} in vt_1.rowedit (my edit form), and added the variable name to my RegEx. As a result, I could add no more / no less than three ‘a’ characters to the field.

    Like

  • The values are not maintained on post-back. If I have an error on one field, it wipes out the values on all the fields. How do I maintain state?

    Like

  • Bill, Sorry, I don’t know enough about code to be able to troubleshoot that. Maybe it’s possible to account for that in the code but I’m not sure.

    Like

  • Shailendra Singh

    Yes Thanks it works for me !!

    Like

  • Thanks for your helpful post, how about validation for choice field? In way you are described,there is no option for choice field in the little chevron at the top right of corner of sharepoint field!

    Like

  • Starluck, No, that’s not possible with this solution. if it’s a choice field, then aren’t they already restricted to only picking from the specified list of choices? What exactly would you need to validate?

    Like

Leave a reply or question

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s