Telephone Number Validation Based on Country in Dynamics 365 Using JavaScript (Web Resource)

Telephone Number Validation Based on Country in Dynamics 365 Using JavaScript (Web Resource)

Introduction

Validating phone numbers in Dynamics 365 is crucial for maintaining data accuracy and ensuring seamless communication with customers. However, phone number formats vary significantly across countries, making validation a challenge.

In this blog post, we’ll explore how to implement country-specific phone number validation in Dynamics 365 using a JavaScript web resource. We’ll leverage the powerful libphonenumber-js library to validate and format phone numbers dynamically based on the selected country.


Why Do We Need Country-Specific Phone Validation?

Phone numbers follow different formats and rules depending on the country. For example:

  • United States: +1 (555) 123-4567
  • United Kingdom: +44 20 7123 4567
  • Germany: +49 30 12345678

Without proper validation, users may enter incorrect or improperly formatted numbers, leading to:

  • Failed communications (SMS, calls)
  • Poor data quality in CRM
  • User frustration due to manual corrections

By implementing automated country-based validation, we ensure:
-> Correct formatting for each country
-> Real-time validation with helpful error messages
-> Auto-formatting for consistent data storage


Implementation Steps

1. Create a JavaScript Web Resource

  • Navigate to Power Apps -> Solutions -> Create New/Or use old one
  • click + New -> More -> Web resource
  • Set the following properties:
    • Name: new_phoneValidation.js
    • Display Name: Phone Number Validation
    • File Type: JavaScript (JS)
  • Paste the provided JavaScript code (see below) or upload the js file
  • Click Save 

Code:


2. Add the Script to a Form

  • Open the form where phone validation is needed (e.g., Contact or Account)
  • Go to Form Libraries → + Add library (add the js – web resouce ) (see the image)
  • Click on the Phone field. Click on Events. Add an OnChange event for the Phone field (telephone1)
  • Select the new_phoneValidation.js library
  • Set the function name to onPhoneNumChange
  • Check “Pass execution context as first parameter” to yes
  • Click Done and Save and publish

3. Understanding the Code

Key Features of the Script:

-> Dynamic Library Loading

-> Validation & Auto-Formatting

-> User-Friendly Error Messages


Testing the Implementation

  1. Open a Contact or Account record.
  2. Select a Country (e.g., “United States”).
  3. Enter an invalid phone number (e.g., 1234567890).
    • Expected: Error message with a valid example.
  4. Enter a valid number (e.g., (555) 123-4567).
    • Expected: Auto-formatted to +1 (555) 123-4567.

Conclusion

By implementing country-specific phone validation, we enhance data quality and user experience in Dynamics 365. The solution:
✔ Dynamically validates based on the selected country
✔ Auto-formats numbers for consistency
✔ Provides helpful feedback for invalid entries

This approach can be extended to support more countries or integrated with other fields.

Would you like a deeper dive into any part of this implementation? Let me know in the comments! 🚀

Keep Learning!

  • Share your thoughts or questions in the comments below.
  • Stay tuned to our blog for more Dynamics 365 tips and tutorials.

Thanks for reading!

Leave a Reply

Discover more from Dynamics Services Group

Subscribe now to keep reading and get access to the full archive.

Continue reading