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

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

Phone number formatting error in CRM 2011

(0) ShareShare
ReportReport
Posted on by 1,905

Hi guys,

I wrote javascript to format the phone number as 111-111-1111, and also if the user didnt put in 10 numbers an alert will pop up. But after I save the phone number, then I clear the field and try to save agai, an error message will pop up says 'Null' is null or not an object. How can I solve this problem cuz user might want to clear the existing phone number.

Below is my code.

function FormatPhoneNumber(context)
{
  var oField = context.getEventSource().getValue();
  var sAllNumeric = oField.replace(/[^0-9]/g, "");
  var sFormattedPhoneNumber;

 if(typeof(oField)!= "undefined" && oField!= null)
  {
   
    switch (sAllNumeric.length)
    {
      case "0833211139".length:
       sFormattedPhoneNumber = sAllNumeric.substring(0,3) + "-"+  sAllNumeric.substring(3,6) +"-"+ sAllNumeric.substring(6)
       break;
   
    default:
    alert("Phone/Fax must contain 10 numbers.")
 break;
   }
    context.getEventSource().setValue(sFormattedPhoneNumber);
  }
 }

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at
    RE: Phone number formatting error in CRM 2011

    You need to catch the null value at the start of your code

    Field = Xrm.Page.getAttribute('phonenumberfield');

    If (field != null){

    ....rest of code here

    }

    Because your code triggers on change when it is cleared it triggers but thee is no value in the field so your .getValue causes an error.

    If you use a debugger tool you should be able to see in future which line in your script is causing the error to assist you to fix

  • Community Member Profile Picture
    on at
    RE: Phone number formatting error in CRM 2011

    your problem is not clear. can you explain what are you looking exactly?

  • David1213 Profile Picture
    1,905 on at
    RE: Phone number formatting error in CRM 2011

    Thank you Andrew & MAk

    Just to explain my problem more clearly. I creared this webresource for all the phone/fax fields that need to be formatted.

    For example, I typed in 222-222-2222 then saved it. It works. Then I cleared the value I just saved (222-222-2222) and tried to save it again, the error message appeared saying Null is null or not an object. But in my code I already defined  if(typeof(oField)!= "undefined" && oField!= null)

  • Community Member Profile Picture
    on at
    RE: Phone number formatting error in CRM 2011

    "Then I cleared the value I just saved (222-222-2222) "

    how do you cleared the value?

    Are you entering once again the value in the text box?

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…

Abhilash Warrier – Community Spotlight

We are honored to recognize Abhilash Warrier as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
HR-09070029-0 Profile Picture

HR-09070029-0 2

#1
UllrSki Profile Picture

UllrSki 2

#3
ED-30091530-0 Profile Picture

ED-30091530-0 1

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans