开发者

CRM 2011 Phone Formatting

开发者 https://www.devze.com 2023-03-18 22:51 出处:网络
Does any one have sample code to apply phone num开发者_运维技巧ber formatting to certain fields???I\'m sort of a JS hack. Help!you should create a library and add the two methods below and then upload

Does any one have sample code to apply phone num开发者_运维技巧ber formatting to certain fields??? I'm sort of a JS hack. Help!


you should create a library and add the two methods below and then upload it as a web resource. Then assign the 'OnPhoneFieldChange' function to the Change event of each field you want to effect

    function OnPhoneFieldChange(context)
{
    var value = context.getEventSource().getValue();
    if (typeof(value) != "undefined" && value != null)
    {
        value = formatPhoneNumber(value);   
    }
    context.getEventSource().setValue(value);
}

function formatPhoneNumber(inputValue) {
    var scrubbed = inputValue.toString().replace(/[^0-9]/g, "");

    var sevenDigitFormat = /^\(?([0-9]{3})[-. ]?([0-9]{4})$/;
    var tenDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
    var extDigitFormat = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})?([0-9]*)$/;
    if (tenDigitFormat.test(scrubbed)) {
        return scrubbed.replace(tenDigitFormat, "($1) $2-$3");
    }
    else if (sevenDigitFormat.test(scrubbed)) {
        return scrubbed.replace(sevenDigitFormat, "$1-$2");
    }
    else if (extDigitFormat.test(scrubbed)) {
        return scrubbed.replace(extDigitFormat, "($1) $2-$3 x$4");
    }
    return inputValue;
}
0

精彩评论

暂无评论...
验证码 换一张
取 消