So I am working on a project and they want to have some customer facing forms. One of the challenges is they want to have some validation/event handling that will ensure the phone format is “Just So”. The main reason is that the field in the SharePoint site needs to be visible to Call Technicians. They also want this information to flow into a CRM system.
What I wanted to do was achieve the perfect (919) 555-1212 format without custom code. So I have three options:
- I can do it in InfoPath
- I can do it in SharePoint
- I can custom code a solution
- Well here is the easiest solution…better it is OOB so no pain, and easy to maintain.
First create a Text(string) Phone Number field, nothing special. I am a fan of making this A required field “since we are doing so much work”. See the Field Properties below for the example.
Then open the Manage Rules on the Ribbon of InfoPath
Next select New > Validation
You should now see this
Now give it a name (Details for:) I like to call it something like Phone Number Validation
Now we need to setup some Conditions, this is ensure the length of the all the numbers come in as a 0123456789 format, so we can properly apply our formatting rule later.
Telephone Number : does not match pattern : Phone number : and
The expression : string-length(translate(., “()- “, “”)) = 10 : and
The expression : string-length(translate(., “()- 0123456789”, “”)) = 0 : and
Now we need to set the format translation, big fan of the ConCat command. You can see it in action.
Action: Set a field’s value
Value: concat(“(“, substring(translate(., “()- “, “”), 1, 3), “) “, substring(translate(., “()- “, “”), 4, 3), “-“, substring(translate(., “()- “, “”), 7, 4))
It should end up looking like this:
So the result looks like this: