Javascript CRM 2011电话格式化

Javascript CRM 2011电话格式化,javascript,dynamics-crm-2011,Javascript,Dynamics Crm 2011,是否有人有将电话号码格式应用于特定字段的示例代码???我有点像JS黑客。救命啊 您应该创建一个库并添加以下两种方法,然后将其作为web资源上载。然后将“OnPhoneFieldChange”函数分配给要生效的每个字段的更改事件 function OnPhoneFieldChange(context) { var value = context.getEventSource().getValue(); if (typeof(value) != "undefined" &am

是否有人有将电话号码格式应用于特定字段的示例代码???我有点像JS黑客。救命啊

您应该创建一个库并添加以下两种方法,然后将其作为web资源上载。然后将“OnPhoneFieldChange”函数分配给要生效的每个字段的更改事件

    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;
}

您应该创建一个库并添加以下两种方法,然后将其作为web资源上载。然后将“OnPhoneFieldChange”函数分配给要生效的每个字段的更改事件

    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;
}