web
You’re offline. This is a read only version of the page.
close
Skip to main content
Community site session details

Community site session details

Session Id :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Phone number validate

(0) ShareShare
ReportReport
Posted on by 1,023

Hi,

We have the phone numbers formatted as (123) 456-7890;ext=12 

I would like to show an error if the phone number contains any other non numbers or special characters. Please advise the code on how to achieve this?

Thanks.

I have the same question (0)
  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at
    RE: Phone number validate

    Hi,

    You can create or use a PCF. See pcf.gallery/.../ for example. A regular expression like \(\d{3}\) \d{3}-\d{4};ext=\d{2} should works.

    Or you can write a javascript code that check the format with a regex:

    const invalidPhoneNumberNotif = "INVALID_PHONE_NUMBER_FORMAT";
    
    function checkPhoneNumberFormat(execCtx) {
        const formCtx = execCtx.getFormContext();
        
        const phoneNumberAttr = execCtx.getEventSource();
        const phoneNumberCtrl = formCtx.getControl(phoneNumberAttr.getName());
        
        const phoneNumber = phoneNumberAttr.getValue();
        const phoneNumberRegex = /\(\d{3}\) \d{3}-\d{4};ext=\d{2}/;
        
        if (phoneNumber != null && !phoneNumberRegex.test(phoneNumber)) {
            phoneNumberCtrl.setNotification("Phone number format must be (123) 456-7890;ext=12", invalidPhoneNumberNotif);
        }
        else {
            phoneNumberCtrl.clearNotification(invalidPhoneNumberNotif);
        }
    }

    Add this javascript function to the "on change" event of your phone number fields. Ensure to check "Pass the execution context as first parameter".

    If ;ext=12 is optional, you will need to adapt the regex. You can test/adapt the regex here: regex101.com/.../1

  • Verified answer
    yify Profile Picture
    1,023 on at
    RE: Phone number validate

    Thanks Xavier, this really helps. In the case where if I would like to allow the phone number EXTENSION maximum from 1 to 10 numbers after ext=, how do I do that in regex? Please advise

    For example, I want the user to allow the phone number (123) 456-7890;ext=12345 or  (123) 456-7890;ext=123412 or (123) 456-7890;ext=12345897

  • Suggested answer
    XM-22040801-0 Profile Picture
    11 on at
    RE: Phone number validate

    In this case, the regex will be \(\d{3}\) \d{3}-\d{4};ext=\d{1,10}

    See the regex in action: regex101.com/.../3

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
#ManoVerse Profile Picture

#ManoVerse 101

#2
Siv Sagar Profile Picture

Siv Sagar 93 Super User 2025 Season 2

#3
Martin Dráb Profile Picture

Martin Dráb 66 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans