Format Phone number field in CRM

Unanswered Format Phone number field in CRM

  • Monday, May 21, 2012 3:26 PM
     
     

    Hello all,I am new to CRM and javascript.

    We have multiple customers through the world. Different country's have different phone number formats.

    I want to set up a jscript that would look at the Country code first. Our country for the us is "USA"

    Then format it (123) 456-7890 any help with this would be great.

All Replies

  • Monday, May 21, 2012 3:42 PM
     
     
    http://blog.stevenlevithan.com/archives/validate-phone-number#r4-3-v-epp could be a good starting point, but you should find a way to handle different formats. For example start with a custom entity storing country code and RegExp(s). This way you can keep your code clean, loading only the RegExp you need.

    Carsten Groth http://carstengroth.wordpress.com Microsoft Dynamics Certified Technology Specialist

  • Monday, May 21, 2012 5:41 PM
     
     
    I don't understand how that jscript works at all.
  • Tuesday, May 22, 2012 3:14 AM
    Moderator
     
     

    If you are brand new to CRM and brand new to JavaScript, then you've picked a real corker of a requirement to learn. (Sorry, I'm British, so "corker" means "seemingly simple, but actually pretty complicated when you research it").

    First of all, you'll find that CRM doesn't have a standard way of determining the country for a record. On the account, contact and address fields, the Country/Region field is just a text field. So the first challenge you have is to standardize the country. Option sets and a Country entity are popular options (I like Carsten's idea to store the country, country code and RegEx in a custom entity, but this doesn't work with the Address entity, so if you need to use the More Addresses feature then you'll have to use an option set instead).

    The CRM 2011 SDK has a short article which provides an example script: http://msdn.microsoft.com/en-us/library/cc468409.aspx, but for international telephone number formatting it gets pretty complicated. Especially when some countries, like the UK, have many different phone number formats for different types of numbers (landline, mobile, free phone, local rate, national rate, etc.).


    Neil Benson, CRM Addict and MVP at Slalom Consulting. Find me on Twitter. Join over 20,000 other CRM professionals on the Microsoft Dynamics CRM group on LinkedIn.

  • Tuesday, May 22, 2012 7:04 AM
     
      Has Code

    Hi Adam,

    I am posting the code to format a US number in 813-949-4100 format. You can tweak with the code and change the regular expression to get the desired result.

     function formatPhoneNumber(Val) {
         try {
    
            if (Val == null) return "";        
            var fmtPhone = (Val).replace(/^\s*|\s*$/g, '');
            fmtPhone = fmtPhone.split(' ').join('');
            fmtPhone = (fmtPhone).replace(/-/g, '');
            if (fmtPhone.length == 10) {
                fmtPhone = fmtPhone.replace(/\D/g, '').replace(/^(\d{3})(\d{3})/, '$1-$2-');
            }
            else if (fmtPhone.length == 7) {
                fmtPhone = fmtPhone.replace(/\D/g, '').replace(/^(\d{3})(\d{4})/, '$1-$2');
            }
            else if (fmtPhone.length == 11) {
                fmtPhone = fmtPhone.replace(/\D/g, '').replace(/^(\d{1})(\d{3})(\d{3})/, '$1-$2-$3-');
            }
    
            return fmtPhone;
        }
        catch (e) {
            alert("Error formatting phone number \"" + Val + "\": " + e.message);
        }
    }
    
    
    function telephone1_onchange() {
        if (Xrm.Page.getAttribute("telephone1").getValue() != null) {
            var isPhone = formatPhoneNumber(Xrm.Page.getAttribute("telephone1").getValue());        
            Xrm.Page.getAttribute("telephone1").setValue(isPhone);
        }
    }

    Hope it helps you.

    Regards,

    Yusuf