Having a consistent entry format for phone numbers is something that most companies want to see in their Dynamics 365 Customer Engagement (CRM) environments.
Scenario 1: User provides data into a phone number field. The field includes some special characters. Cleaning them becomes a difficult task.
Scenario 2: If there are more than 10 digits provided in a Phone Number field.
Scenario 3: Format a Phone Number to desired Format.
In Dynamics 365 on the form where we need to have this functionality add this code on change of the Phone Number and on load and on save of the Form.
1. Clear all the special characters provided in a Phone Number field.
Create a variable and get the value of the phone number field. Use Replace function to clear special characters.
2. Throw error when more than 10 digits are provided.
Write a condition checking if the variable’s length is not equals to 10. Set the focus of the field to the required field and throw an alert mentioning the Phone Number must contain 10 numbers / digits.
3. Format automatically based on a pre-defined template. E.g., (123) 456-7890
Once the Phone Number is set to have 10 digits. Then using “substr” function add opening bracket, space, hyphen and closing bracket at the required positions.