Skip to main content

Notifications

Microsoft Dynamics CRM (Archived)

Auto Formatting Phone Numbers in Leads Forms

Posted on by Microsoft Employee

I know that I need to use JavaScript for this, but I really don't know where to start. I need to auto format the input of numbers to '1 (xxx) xxx-xxxx ' Some code snippets or something would be helpful. I'm a beginner with JS and CRM. Thanks!

*This post is locked for comments

  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Auto Formatting Phone Numbers in Leads Forms

    Add

    Xrm.Utility.alertDialog("onchange of phone number");

    Inside the first function and check that you are getting alert message when you modify the phone field then click another field in the form.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Formatting Phone Numbers in Leads Forms

    It didn't format anything at all when I added numbers in. I really don't know what went wrong here. It's added as on onchange event to the Business Phone number field and it's not doing anything to the number. I've saved and published everything. Thanks

  • Suggested answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Auto Formatting Phone Numbers in Leads Forms
    function formatPhoneNumber(context) {
        var phoneNumber = context.getEventSource();
    
        // Verify that the field is valid
        if (typeof (phoneNumber) != "undefined" && phoneNumber != null) {
            if (phoneNumber.getValue() != null) {
                // Remove any special characters
                var sTmp = phoneNumber.getValue().replace(/[^0-9,A-Z,a-z]/g, "");
    
                // Translate any letters to the equivalent phone number, if method is included
                try {
                    if (sTmp.length <= 10) {
                        sTmp = TranslateMask(sTmp);
                    }
                                    else {
                        sTmp = TranslateMask(sTmp.substr(0, 10)) + sTmp.substr(10, sTmp.length);
                    }
                }
                catch (e) {
                }
    
                // If the number is a length we expect and support, 
                // format the translated number
                switch (sTmp.length) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 8:
                    case 9:
                        break;
                    case 7:
                        phoneNumber.setValue(sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4));
                        break;
                    case 10:
                        phoneNumber.setValue("(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4));
                        break;
                    default:
                        phoneNumber.setValue("(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4) + " " + sTmp.substr(10, sTmp.length));
                        break;
                }
            }
        }
    }
    
    /// lt;summarygt;
    /// TranslateMask() will step through each character of an 
    /// input string and pass that character to the 
    /// TranslatePhoneLetter() helper method
    /// lt;/summarygt;
    /// lt;param name="s"gt;Input string to translatelt;/paramgt;
    function TranslateMask(s) {
        var ret = "";
    
        //loop through each char, and pass it to the translation method
        for (var i = 0; i < s.length; i++)
        {
            ret += TranslatePhoneLetter(s.charAt(i))
    
        }
    
        return ret;
    }
    
    /// lt;summarygt;
    /// TranslatePhoneLetter() takes a character and returns the 
    /// equivalent phone number digit if it is alphanumeric
    /// lt;/summarygt;
    /// lt;param name="s"gt;Character to translatelt;/paramgt;
    function TranslatePhoneLetter(s) {
        var sTmp = s.toUpperCase();
        var ret = s;
    
        switch (sTmp) {
            case "A":
            case "B":
            case "C":
                ret = 2;
                break;
            case "D":
            case "E":
            case "F":
                ret = 3;
                break;
            case "G":
            case "H":
            case "I":
                ret = 4;
                break;
            case "J":
            case "K":
            case "L":
                ret = 5;
                break;
            case "M":
            case "N":
            case "O":
                ret = 6;
                break;
            case "P":
            case "Q":
            case "R":
            case "S":
                ret = 7;
                break;
            case "T":
            case "U":
            case "V":
                ret = 8;
                break;
            case "W":
            case "X":
            case "Y":
            case "Z":
                ret = 9;
                break;
            default:
                ret = s;
                break;
    
        }
    
        return ret;
    }
  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Formatting Phone Numbers in Leads Forms
    //I removed all of the & symbols. I'm not sure if that's what I was supposed to do.

    function formatPhoneNumber(context) {
        var phoneNumber = context.getEventSource();
     
        // Verify that the field is valid
        if (typeof (phoneNumber) != "undefined" <)amp;amp; phoneNumber != null) {
            if (phoneNumber.getValue() != null) {
                // Remove any special characters
                var sTmp = phoneNumber.getValue().replace(/[^0-9,A-Z,a-z]/g, "");
     
                // Translate any letters to the equivalent phone number, if method is included
                try {
                    if (sTmp.length lt;= 10) {
                        sTmp = TranslateMask(sTmp);
                    }
                    else {
                        sTmp = TranslateMask(sTmp.substr(0, 10)) + sTmp.substr(10, sTmp.length);
                    }
                }
                catch (e) {
                }
     
                // If the number is a length we expect and support, 
                // format the translated number
                switch (sTmp.length) {
                    case 1:
                    case 2:
                    case 3:
                    case 4:
                    case 5:
                    case 6:
                    case 8:
                    case 9:
                        break;
                    case 7:
                        phoneNumber.setValue(sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4));
                        break;
                    case 10:
                        phoneNumber.setValue("(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4));
                        break;
                    default:
                        phoneNumber.setValue("(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4) + " " + sTmp.substr(10, sTmp.length));
                        break;
                }
            }
        }
    }
     
    /// lt;summarygt;
    /// TranslateMask() will step through each character of an 
    /// input string and pass that character to the 
    /// TranslatePhoneLetter() helper method
    /// lt;/summarygt;
    /// lt;param name="s"gt;Input string to translatelt;/paramgt;
    function TranslateMask( s )
    {
      var ret = "";
     
      //loop through each char, and pass it to the translation method
      for (var i=0; ilt;s.length; i++) 
      {
        ret += TranslatePhoneLetter(s.charAt(i))
      }
     
      return ret;
    }
     
    /// lt;summarygt;
    /// TranslatePhoneLetter() takes a character and returns the 
    /// equivalent phone number digit if it is alphanumeric
    /// lt;/summarygt;
    /// lt;param name="s"gt;Character to translatelt;/paramgt;
    function TranslatePhoneLetter( s )
    {
     var sTmp = s.toUpperCase();
     var ret = s;
     
     switch( sTmp )
     {
     case "A":
     case "B":
     case "C": 
      ret = 2;
      break;
     case "D":
     case "E":
     case "F": 
      ret = 3;
      break;
     case "G":
     case "H":
     case "I": 
      ret = 4;
      break;
     case "J":
     case "K":
     case "L": 
      ret = 5;
      break;
     case "M":
     case "N":
     case "O": 
      ret = 6;
      break;
     case "P":
     case "Q":
     case "R":
     case "S": 
      ret = 7;
      break;
     case "T":
     case "U":
     case "V": 
      ret = 8;
      break;
     case "W":
     case "X":
     case "Y":
     case "Z": 
      ret = 9;
      break;
     default: 
      ret = s;
      break;
     }
     
     return ret;
    }
  • Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Auto Formatting Phone Numbers in Leads Forms

    When I copied into my VS I found there were many special characters in the code.

    Would you please copy the code here, so that I can check it.

    Use 'Use Rich formatting' option when you reply and use the code pasting option button

    </>

    in the menu and paste the code please.

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Formatting Phone Numbers in Leads Forms

    Are you saying to replace the & symbol with the <) ?

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Auto Formatting Phone Numbers in Leads Forms

    Please check the code in Visual Studio or any JS editor.

    Remove any special characters

    (remove & lt; with <)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Formatting Phone Numbers in Leads Forms

    One of the scripts for this record has caused an error. For more details, download the log file.

    ReferenceError: auto is not defined at eval (eval at RunHandlerInternal (bleauprints.crm.dynamics.com/.../ClientApiWrapper.aspx), <anonymous>:1:1)

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Auto Formatting Phone Numbers in Leads Forms

    I did however the code isn't working. I added the event to the field and it's not doing anything.

  • Verified answer
    Gopalan Bhuvanesh Profile Picture
    Gopalan Bhuvanesh 11,397 on at
    RE: Auto Formatting Phone Numbers in Leads Forms

    This is within the form, for the onChange event of the field where you want to format the value.

    Please check the screenshots in that article.

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!

Community AMA December 12th

Join us as we continue to demystify the Dynamics 365 Contact Center

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,149 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans