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

Notifications

Announcements

No record found.

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

    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

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

  • David1213 Profile Picture
    1,905 on at

    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

    "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…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans