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

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Phone Number Validation on Dynamics CRM 365 forms

(0) ShareShare
ReportReport
Posted on by

I need to put UK mobile number validation on a field. Can someone share a working JavaScript?

*This post is locked for comments

I have the same question (0)
  • Verified answer
    Mahendar Pal Profile Picture
    45,095 on at

    Hi Snitika,

    Did you check existing forum posts like below:

    social.microsoft.com/.../dynamics-crm-2011-uk-phone-number-javascript-validation

    community.dynamics.com/.../119483

    These should help you.

  • Suggested answer
    Aric Levin - MVP Profile Picture
    30,190 Moderator on at

    Take a look at the following post.

    community.dynamics.com/.../formatting-phone-number

    Maybe it will help.

  • Community Member Profile Picture
    on at

    272874.Capture.PNGHere is my code which keeps giving me Script Error

    function formatPhoneNumber()

    {

       var phoneNumber = Xrm.Page.getAttribute("mobilephone").getValue()

       var fixedNumber = string.replace(/[^0-9]/g, '');

       var output = "";

       switch (fixedNumber.length)

       {

           case 7:

               output = fixedNumber.substring(0, 3) + "-" + fixedNumber.substring(4, 7);

               break;

           case 10:

               output = "(" + fixedNumber.substring(0, 3) + ") " + fixedNumber.substring(4, 6) + "-" + fixedNumber.substring(7, 10);

               break;

           case 11:

               if (fixedNumber.substring(0, 1) == "1")

                   output = "(" + fixedNumber.substring(1, 4) + ") " + fixedNumber.substring(5, 7) + " " + fixedNumber.substring(8, 11);

               else

                   output = fixedNumber;

               break;

           default:

               // Error No Output

               output = phoneNumber

               break;

       }

       Xrm.Page.getAttribute("mobilephone").setValue(output);

    }

  • Verified answer
    gdas Profile Picture
    50,091 Moderator on at

    Hi Snitika,

    I have corrected your code , please try to run -

            function formatPhoneNumber() {
                var phoneNumber = Xrm.Page.getAttribute("mobilephone").getValue();
                var fixedNumber = phoneNumber.replace(/[^0-9]/g, '');
                var output = "";
                switch (fixedNumber.length) {
                    case 7:
                        output = fixedNumber.substring(0, 3) + "-" + fixedNumber.substring(4, 7);
                        break;
                    case 10:
                        output = "(" + fixedNumber.substring(0, 3) + ") " + fixedNumber.substring(4, 6) + "-" + fixedNumber.substring(7, 10);
                        break;
                    case 11:
                        if (fixedNumber.substring(0, 1) == "1")
                            output = "(" + fixedNumber.substring(1, 4) + ") " + fixedNumber.substring(5, 7) + " " + fixedNumber.substring(8, 11);
                        else
                            output = fixedNumber;
                        break;
                    default:
                        // Error No Output
                        output = phoneNumber
                        break;
    
                }
                Xrm.Page.getAttribute("mobilephone").setValue(output);
    
            }


  • Suggested answer
    Mahendar Pal Profile Picture
    45,095 on at

    Nothing wrong with the code working fine just change string to phonenumber.

  • Suggested answer
    gdas Profile Picture
    50,091 Moderator on at

    Yes Mahendar you are correct , I did only highlighted one , it was "string"  which should be phone number.

  • Community Member Profile Picture
    on at

    Hey Snitika,

    For phone number validation or more thing about this number, I use this code below:

    var phone = 'Phone number';
    var key = 'Your API key';
    var default_country = 'Country code';
    $.ajax({
        url: 'https://api.veriphone.io/v2/verify',
        method: 'POST',
        data:{phone: phone, key: key, default_country: default_country},   
        dataType: 'json',
        success: function(data) {
            console.log(data.phone_valid);   
            console.log(data.country);
            console.log(data.international_number);  
            console.log(data.carrier);         
        }
    });

    And you can get your API key in this link Veriphone

  • Suggested answer
    Kokulan Profile Picture
    18,054 on at

    You could also consider using one of the following PCF controls to validate phone number or other fields.

    ScreenClip-_5B00_20_5D00_.pngScreenClip-_5B00_21_5D00_.png

    https://pcf.gallery/categories#regex

    https://pcf.gallery/check-phone-number-control/

  • Suggested answer
    Prudvihub Profile Picture
    65 on at

    Look at the following post

    prudvihub.blogspot.com/.../mobile-number-validation-in-microsoftcrm.html

  • Beginner Developer Profile Picture
    55 on at

    HI All,

    I have requirement to format the phone number based on country for ex : if CA and US +1 and format the phone number and if exceed 10 digit then exclude country code .

    Thanks in advance  

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans