Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Auto formatting phone number to 123.456.7890

Posted on by

Hi All,

My requirement is to format the user entered phone number to this format 123.456.7890

If the user enters numbers less than 10, I have to show a validation message.

I am new to CRM and javascript. I have done some R & D but this type of formatting. havent found.

Any help for this code  and steps doing this will be very helpful to me.

*This post is locked for comments

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Auto formatting phone number to 123.456.7890

    Dom Editor error?

    You added your custom code only right??

    And check whether JS is triggering or not and if it is triggering then debug where exactly you got this error..

  • RE: Auto formatting phone number to 123.456.7890

    Hi Nick, Thanks for the post. But I am new to CRM and javascript and I m not sure where I have to modify the code.

    My only requirement is when user enters any phone number  for ex 8723265348, system should format it to 872.326.5348.

    It will be great if you can help me with that code.

    Thank you so much

  • RE: Auto formatting phone number to 123.456.7890

    I have used your code and below is the error I am getting and I did this on onchange.

    Please suggest

    function SetMobilePhone() {

       var phonenumber = Xrm.Page.getAttribute("mobilephone").getValue();

       if (phonenumber != null) {

           if (phonenumber.length != 10)

               alert("messgae")

           phonenumber.setValue(phonenumber.substr(0, 3) + ". " + phonenumber.substr(3, 3) + "." + phonenumber.substr(6, 4));

       }

    }

    SetMobilePhone@mscrm.crm.dynamics.com/.../new_SetMobilePhone

    @mscrm.crm.dynamics.com/.../ClientApiWrapper.aspx line 158 > eval:1:1

    RunHandlerInternal@mscrm.crm.dynamics.com/.../ClientApiWrapper.aspx

    RunHandlers@mscrm.crm.dynamics.com/.../ClientApiWrapper.aspx

    ExecuteHandler@mscrm.crm.dynamics.com/.../ClientApiWrapper.aspx

    Mscrm.TurboForm.Control.CustomScriptsManager.prototype.$C7_1@mscrm.crm.dynamics.com/.../JsProvider.ashx;ids=1223231347-405283869:8911:17

    Mscrm.TurboForm.Control.CustomScriptsManager.prototype.executeHandler@mscrm.crm.dynamics.com/.../JsProvider.ashx;ids=1223231347-405283869:8857:13

    Mscrm.TurboForm.Control.CustomScriptsManager.prototype.executeHandlerByDescriptor@mscrm.crm.dynamics.com/.../JsProvider.ashx;ids=1223231347-405283869:8888:13

    Mscrm.TurboForm.Control.CustomScriptsManager.prototype.getHandler/<@mscrm.crm.dynamics.com/.../JsProvider.ashx;ids=1223231347-405283869:8896:13

    Mscrm.ClientApiEventHandlerList.prototype.getHandler/<@mscrm.crm.dynamics.com/.../global.ashx

    Mscrm.TurboForm.Control.Data.DataAttributeBase.prototype.fireOnChange@mscrm.crm.dynamics.com/.../JsProvider.ashx;ids=1223231347-405283869:16731:13

    Mscrm.TurboForm.Control.Data.DataAttributeBase.prototype.setValueInternal@mscrm.crm.dynamics.com/.../JsProvider.ashx;ids=1223231347-405283869:16838:21

    Mscrm.TurboForm.Control.Data.DataAttributeBase.prototype.setValue@mscrm.crm.dynamics.com/.../JsProvider.ashx;ids=1223231347-405283869:16815:13

    Mscrm.TurboForm.Control.View.AutoLookupEditElement.prototype.$3P_2@mscrm.crm.dynamics.com/.../JsProvider.ashx;ids=1223231347-405283869:29477:9

    Function.createDelegate/<@mscrm.crm.dynamics.com/.../global.ashx

    Sys.UI.DomEvent.addHandler/b@mscrm.crm.dynamics.com/.../global.ashx

  • shivaram Profile Picture
    shivaram 3,315 on at
    RE: Auto formatting phone number to 123.456.7890

    Based on your requirement.

    If you want this on Change of phone event or On Save event???

    No need to pass any parameters.. Just call the function OnChange or On Save(based on Requirement)

  • Suggested answer
    Nick.Doelman Profile Picture
    Nick.Doelman 1,947 Most Valuable Professional on at
    RE: Auto formatting phone number to 123.456.7890

    Here example of some code that you can attach to the onchange event of any phone number field.

    It will prompt the user based on language of interface, and if there are extra digits (for extensions, they can be separated by an "*"

    This came from a blog posting my team did 5 years ago, but it still applies to CRM 2016.

    • The function must beadded to a web resource accessible to your form, and coupled with an onChange event on the phone number field itself. For example, the following would format the main phone field on the Account form >

    function telephone1_onchange()
            {
                PhoneFormat("telephone1");
            }

    • We use the out-of-box address1_country field to determine if the phone number should or shouldn't be formatted. If you're using a custom field for the country, make sure to modify the code accordingly.

    //------Format a 10-digit phone number to 'xxx xxx-xxxx', with the option to add stuff after the asteriskfunction PhoneFormat(oField) {
        var field = Xrm.Page.getAttribute(oField);
        if (field.getValue() != "undefined" && field.getValue() != null) {
            var country = Xrm.Page.getAttribute("address1_country").getValue();
            if (country == "Canada" || country == "United States" || country == "Bermuda" || country == "Mexico") {
                var sTmp = field.getValue().replace(/[^0-9,*]/g, "");
                if (sTmp.length == 10) {
                    field.setValue(sTmp.substr(0, 3) + " " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4));
                    field.setSubmitMode("always");
                }
                else {
                    if (sTmp.length > 10) {
                        if (field.getValue().search(/[*]/g) > -1) {
                            field.setValue(sTmp.substr(0, 3) + " " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4) + " " + field.getValue().substring(field.getValue().search(/[*]/g)));
                        }
                        else {
                            field.setValue(sTmp.substr(0, 3) + " " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4) + " ext. " + sTmp.substring(10));
                        }
                    }
                    else {
                        if (Xrm.Page.context.getUserLcid() == 1033) {
                            alert("This number should contain 10 digits. Please verify the data entered.");
                        }
                        if (Xrm.Page.context.getUserLcid() == 1036) {
                            alert("Ce numéro devrait contenir 10 chiffres. Veuillez vérifier et faire les ajustements nécessaires.");
                        }
                    }
                }
            }
        }
    }

  • RE: Auto formatting phone number to 123.456.7890

    Hi Shivaram,

    Should I register this on on change event and should I pass any paprameters?

    Please suggest

  • Suggested answer
    shivaram Profile Picture
    shivaram 3,315 on at
    RE: Auto formatting phone number to 123.456.7890

    Hi

    You can use following sample code

    var phonenumber=Xrm.Page.getAttribute("At.Name").getValue();

    if(phonenumber!=null){

    if(phonenumber.length!=10)

    alert("messgae")

    phone.setValue( phone.substr(0, 3) + ". " + phone.substr(3, 3) + "." + phone.substr(6, 4));

       }

    }

    Hope it helps you

    Thanks.

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,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans