locked
Formatting phone numbers... RRS feed

  • Question

  • If I already have phone numbers input into the CRM and want to format them, no matter how they come in, as

    (###) ###-####

    And also have any new entries follow the same format, how would I do that and can it be done? I'm on Dynamic CRM 2011.


    • Edited by nasyrax Thursday, June 5, 2014 2:57 PM
    Thursday, June 5, 2014 2:49 PM

Answers

  • Pls Try this

    var scrubbed = Xrm.Page.getAttribute("xxx").getValue().toString().replace(/[^0-9]/g, "");
    
    var tenDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{2})[-. ]?([0-9]{2})$/;
    
    var digitedformat = null;
      if (tenDigitFormat.test(scrubbed)) {
            digitedformat= scrubbed.replace(tenDigitFormat, "($1) $2-$3$4");
     
        }

    Mark Answered if it is helpful for you
    Thursday, June 5, 2014 3:37 PM

All replies

  • Hi,

    If you already have the phone numbers in existing records and want to hide them from users, you can enable Field Level Security in CRM 2011. That would prevent the users from seeing the phone numbers anywhere in CRM, in forms, in advanced finds, etc. Have a look here : http://www.powerobjects.com/blog/2010/10/08/field-level-security-out-of-box-in-microsoft-dynamics-crm-2011/

    I would not recommend masking the number using JavaScript since users can find it using Advanced Find and other ways. Field Level Security would be the best option.


    Admin QuikView Solution for CRM 2013

    Thursday, June 5, 2014 3:18 PM
  • Sorry, I think you may have misunderstood what I meant. If someone inputs the following

    8885551212

    888-555-1212

    (888)555-1212

    it would format it to 

    (888) 555-1212

    I overlooked the fact that it looked like I wanted the field masked  :0)


    • Edited by nasyrax Thursday, June 5, 2014 3:28 PM
    Thursday, June 5, 2014 3:28 PM
  • Pls Try this

    var scrubbed = Xrm.Page.getAttribute("xxx").getValue().toString().replace(/[^0-9]/g, "");
    
    var tenDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{2})[-. ]?([0-9]{2})$/;
    
    var digitedformat = null;
      if (tenDigitFormat.test(scrubbed)) {
            digitedformat= scrubbed.replace(tenDigitFormat, "($1) $2-$3$4");
     
        }

    Mark Answered if it is helpful for you
    Thursday, June 5, 2014 3:37 PM
  • Please excuse my n00bnes, but how do I run that script?
    Thursday, June 5, 2014 6:25 PM
  • You need to call this script to your phone number field onchange event. After writing this you need to set this to your field also
    var scrubbed = Xrm.Page.getAttribute("yourfieldname").getValue().toString().replace(/[^0-9]/g, "");
    
    var tenDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{2})[-. ]?([0-9]{2})$/;
    
    var digitedformat = null;
      if (tenDigitFormat.test(scrubbed)) {
            digitedformat= scrubbed.replace(tenDigitFormat, "($1) $2-$3$4");
     Xrm.Page.getAttribute("xxx").setValue("yourfieldname")
        }

    Thursday, June 5, 2014 7:43 PM
  • I get an error, "Object Expected". I created a Web Resource with the type set to JScript, then go to my field to the events tab, select the web resource in the Form Library, then add it to the OnChange event with the Function set to "scrubbed".

    I have my script as follows:

    var scrubbed = Xrm.Page.getAttribute("mobilephone").getValue().toString().replace(/[^0-9]/g, ""); var tenDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{2})[-. ]?([0-9]{2})$/; var digitedformat = null; if (tenDigitFormat.test(scrubbed)) { digitedformat= scrubbed.replace(tenDigitFormat, "($1) $2-$3$4"); Xrm.Page.getAttribute("xxx").setValue("mobilephone") }

    Thursday, June 5, 2014 8:07 PM
  • in your Javascript file did you get your code into the scrubbed function

    function scrubbed()
    
    {var scrubbed = Xrm.Page.getAttribute("mobilephone").getValue().toString().replace(/[^0-9]/g,
    "");
    var
     tenDigitFormat =/^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{2})[-. ]?([0-9]{2})$/;
    var digitedformat =null;
    if(tenDigitFormat.test(scrubbed))
    {
     digitedformat= scrubbed.replace(tenDigitFormat,"($1) $2-$3$4");
    Xrm.Page.getAttribute("xxx").setValue("mobilephone")
    }
    }

    You can google it how to call function onchange in crm 2011 , this is very basic:)

    Thursday, June 5, 2014 8:40 PM
  • Hi,

    You have an error in this line:

    Xrm.Page.getAttribute("xxx").setValue("mobilephone")

    It should be :

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

    So, Polat's complete code would look like

    function formatMobileNumber()
    {
    	var scrubbed = Xrm.Page.getAttribute("mobilephone").getValue().toString().replace(/[^0-9]/g, "");
    
    	var tenDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{2})[-. ]?([0-9]{2})$/;
    
    	var digitedformat = null;
    	if (tenDigitFormat.test(scrubbed)) 
    	{
    		digitedformat= scrubbed.replace(tenDigitFormat, "($1) $2-$3$4");
    		Xrm.Page.getAttribute("mobilephone").setValue(digitedformat);
    	}
    }


    Admin QuikView Solution for CRM 2013

    Friday, June 6, 2014 6:06 AM