• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1967
  • Last Modified:

CRM 4.0 Phone Numbers w/Extensions Formatting

I'm still trying to learn JavaScript and need some assistance to figure out a way to allow Users to enter a phone number with extension included.

I'm wondering if their is a way to format a phone number based on a few different conditions.
1.  If a phone number has a first digit of a '1' and the number of digits >=13 then
format the number as  either:
1 (###) ###-####
or
1 (###) ###-#### x###
This way any numbers after the 13th digit will have a space and "x" prior

2. If a phone number has a length of >=10 digits
This way any numbers after the tenth digit could be formatted to:
Example: (###) ###-####
Example: (###) ###-#### x##

3. If a phone number has a length of 7 digits, format to:
Example ###-####

I have included code of where I currently stand with this code.  The code is being used for the OnChange Event, and a OnSave Event for a specified field.

I appreciate your help in advance.
OnChange - Telephone field
{
var oField = event.srcElement;
 
// Validate the field information.
if (oField.DataValue != "undefined" && oField.DataValue != null)
{
// Remove any nonnumeric characters.
var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
 
// If the number has a valid length, format the number.
switch (sTmp.length)
{
	case "4105551212".length:
	oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
	break;
 
	case "5551212".length:
       oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
       alert("The Phone number entered does not contain a Zip Code.");
	break;
 
                     case "14105551212".length:
	oField.DataValue = sTmp.substr(0,1) + " " + "(" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4);
	break;
 
	default:
	alert("The Phone\Fax number entered is invalid, please format as (724) 387-5200");
	break;
}
}
 
/////////////////////////////////////////////////////////////////////////////////////////////////////
 
OnSave - Telephone Field
PhoneValidation(crmForm.all.telephone1);
 
function PhoneValidation(oField)
{
	// Validate the field information.
	if (oField.DataValue != "undefined" && oField.DataValue != null)
	{
	// Remove any nonnumeric characters.
	var sTmp = oField.DataValue.replace(/[^0-9]/g, "");
 
	switch (sTmp.length)
	{
	
        case "4105551212".length:
	oField.DataValue = "(" + sTmp.substr(0, 3) + ") " + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
	break;
 
	case "5551212".length:
	oField.DataValue = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
	break;
 
        case "14105551212".length:
	oField.DataValue = sTmp.substr(0,1) + " " + "(" + sTmp.substr(1, 3) + ") " + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4);
	break;
 
      	default:
	alert("The Phone/Fax number entered is invalid, please format as (724) 387-5200");
	event.returnValue = false;
	oField.focus();
	break;
	}
	}
}

Open in new window

0
mdw233
Asked:
mdw233
  • 2
1 Solution
 
BadotzCommented:
You strip all non-numeric chars from the phone number already. Then, if the length of the resulting string is not 10, it is invalid (according to your code). Force them to enter the area code along with the phone number.
0
 
crm_infoCommented:
Try this.

Put the first code snippet into your OnLoad event of your form (that way you don't have to copy it into each individual phone field).

Call the code snippet from the OnChange event of each phone number, such as:
crmForm.all.phone.DataValue = FormatPhone(crmForm.all.phone.DataValue);

That should meet all of your needs.  A couple of quick notes:
* This will convert any number with 11 digits into a number with a leading "1" (i..e. 27045551212) would be converted into 1(704)555-1212.  A quick addition to the logic should handle that for you.
* I removed all spaces before the extension to support one-click dialing on a Windows Mobile device (see: http://www.askcrm.com/Home/tabid/866/EntryID/75/Default.aspx)
* This will only work if the user enters an "x" before the extension ... I think this appraoch works a bit better than assuming that any "extra" digits are an extension.

That should do the trick for you!
FormatPhone = function(PhoneNum) {
 
var tmpPhone = PhoneNum;
 
// Remove and save the Extension
var hasExt = false;
  if (PhoneNum.indexOf("x") > 0) {
    var extStr = PhoneNum.split("x");
    hasExt = true;
    PhoneNum = extStr[0];
  }
 
// Remove any nonnumeric characters.
  var sTmp = PhoneNum.replace(/[^0-9]/g, "");
  var NewPhone;
 
// If the number has a valid length, format the number.
  switch (sTmp.length)
  {
    case "17048952500".length:
      NewPhone = "1(" + sTmp.substr(1, 3) + ")" + sTmp.substr(4, 3) + "-" + sTmp.substr(7, 4);
      break;
 
    case "7048952500".length:
      NewPhone = "(" + sTmp.substr(0, 3) + ")" + sTmp.substr(3, 3) + "-" + sTmp.substr(6, 4);
      break;
 
    case "8952500".length:
      NewPhone = sTmp.substr(0, 3) + "-" + sTmp.substr(3, 4);
      break;
  }
 
if (NewPhone != null) {
  if (hasExt) {
    NewPhone = NewPhone + " x" + extStr[1];
  }}
  else  {
    NewPhone = tmpPhone;
}
 
return (NewPhone);
 
}

Open in new window

0
 
crm_infoCommented:
I developed the code just for this poster and deployed it to our own CRM.  It works.
0
 
ApexCoCommented:
I attempted to deploy this code and it's giving me an object expected error on load.

Wondering what I'm missing.


Thanks.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now