Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics 365 | Integration, Dataverse...
Answered

Phone number validate

Posted on by 1,017

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.

  • Suggested answer
    XM-22040801-0 Profile Picture
    XM-22040801-0 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

  • Verified answer
    yify Profile Picture
    yify 1,017 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
    XM-22040801-0 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

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,269 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,198 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Product updates

Dynamics 365 release plans