locked
CRM 2011 Allowing Telephone Number Field to Auto Format Domestic But Allow International Numbers RRS feed

  • Question

  • Hello Community,

    I am currently working with a custom piece of JavaScript in CRM 2011 that auto formats 10 digit phone numbers:

    function

    checkPhoneNumberFormat(obj) {

    varphoneNumber = obj.getEventSource().getValue();

    if(phoneNumber != null) {

    varsTmp = phoneNumber.replace(/[^0-9]/g, "");

    switch(sTmp.length) {

    case10:

    obj.getEventSource().setValue(

    "("+ sTmp.substr(0, 3) + ") "+ sTmp.substr(3, 3) + "-"+ sTmp.substr(6, 4));

    break;

    default:

    alert(

    "Phone must contain 10 numbers.");

    break;

    }

    }

    }


     Preforming this auto format is just fine, But how could I get this Script to allow up to 14 digit phone numbers and Also leave anything above 10 digit numbers in a continuous string? I'd also need it to not throw an error that the Phone number is too long.

    Thank You!

    Tuesday, May 20, 2014 3:41 PM

Answers

  • Here is the one that I use.
    try {
    		var re = /^[^\w]*((1[^\d]*)?([2-9]\d{2}))?[^\w]*([2-9]\d{2})[^\w]*(\d{4})\s*((x|ext|ext\.|extension)\s*(\d+))?\s*([a-z].+)?$/i;
    		var arr = re.exec(pn);
    		if (arr != null) {
    			/** ************************************ **
    			** reg expr result array elements:
    			** 1 = area code segment, including optional leading 1
    			** 2 = optional leading 1 (part of area code segment)
    			** 3 = 3-digit area code (part of area code segment, cannot start with 0 or 1)
    			** 4 = 3-digit exchange segment (cannot start with 0 or 1)
    			** 5 = 4-digit local number segment
    			** 6 = extension segment, including prefix
    			** 7 = extension prefix ("x", "ext", "ext." or "extension")
    			** 8 = extension digits
    			** 9 = additional characters
    			** ************************************* */
    
    			var ret = '';
    			if (arr[1] && arr[1] != '' && arr[3] && arr[3] != '')
    				ret += '(' + arr[3] + ') '
    			ret += arr[4] + '-' + arr[5];
    			if (arr[6] && arr[6] != '' && arr[8] && arr[8] != '') {
    				ret += ' x' + arr[8];
    			}
    			if (arr[9] && arr[9] != '') {
    				ret += ' ' + arr[9];
    			}
    			return ret;
    		}
    		else {
    			return pn;
    		}
    	} catch (e) {
    		/** show an error */
    		alert('DPTFormatPhone: An error occurred while attempting to format the phone number value: \'' + pn + '\')\n' + e.message);
    	}
    }

    • Marked as answer by PawelGM Thursday, May 22, 2014 12:40 PM
    Wednesday, May 21, 2014 8:15 PM