Site icon @WonderLaura

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:
  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.
  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:
  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:
    Here’s what the Tag Properties pane looks like when the Regular Expression Validator is selected:
    THIS 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:

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:

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

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.

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:

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

Regular Expression Library

Exit mobile version