Skip to content

Filter freemail provider on ClickDimensions forms

Posted in ClickDimensions, Dynamics 365, Power Platform, and Revive

Currently I have the request to filter freemail provider on ClickDimensions forms, because the customer wants only B2B contacts in his database.

ClickDimensions already provides a setting to filter freemail provider and it would be great first step to met the requirement if the editor would save the setting correctly.

At the moment, everytime you reopen the editor the above setting is unchecked and it doesn’t work in the form.

Apart from that, only four freemail providers are offered to filter.
That’s why I took a look into the Javascripts of ClickDimensions and found and extended the original code for it.

The following code will override the original code to add an additional validation. You can add other or even more freemail providers to met your requirements. Just insert it in the CodeEditor of the forms designer.

// enable or disable the filter functionality
var filterFreemail = "true";

// customize the message provided to the user when a freemail address has been entered
var clickd_MSG_FREE_EMAIL = "We accept only business email addresses.";

// customize the array of blocked freemail providers to met your requirements
var blockedFreeEmail = ["hotmail.com", "gmail.com", "yahoo.com", "aol.com"];

// default ClickDimensions function, extended with our own validation
function FormValid()
{
    var isValid = true,
        isAllowed = true,
        reqFieldList = clickd_jquery("input[name='reqField']", "#clickdimensionsForm");

    for (var i = 0; i < reqFieldList.length; i++)
    {
        // default validation
        isValid = ValidField(clickd_jquery(reqFieldList[i]));
        if (!isValid)
        {
            break;
        }

        // custom validation
        isAllowed = AllowedField(clickd_jquery(reqFieldList[i]));
        if (!isAllowed)
        {
            isValid = false;
            break;
        }
    }
    return isValid;
}

// own validation, copied and modified from ClickDimensions original
function AllowedField(hidden)
{
    var fieldType = hidden.attr("alt");
    var fieldID = hidden.val();
    var fieldString = "";

    var field = clickd_jquery("#" + fieldID);
    if (clickd_jquery("#cont_id_" + fieldID).attr("wasskipped") == "1")
    {
        return true;
    }

    var infoId = "required_info_" + fieldID;
    infoId = infoId.replace("f_upload", "f");
    var info = clickd_jquery("#" + infoId);

    var infoText = clickd_jquery(info).text();
    if (infoText != "")
    {
        Un_SelectNotValidInput(info, field);
    }


    if (fieldType.toLowerCase() == "email")
    {
        fieldString = clickd_jquery(field).val();

        if (fieldString.length > 0)
        {
            fieldString = fieldString.replace(/\s+$/gm, '');
        }
    }
    
    if (fieldType.toLowerCase() == "email" && fieldString.length > 0)
    {
        var domain = fieldString.split("@")[1].toLowerCase();

        // set field invalid for freemail provider
        if (filterFreemail.toLowerCase() == "true")            
        {
            for (var i = 0; i < blockedFreeEmail.length; i++)
            {
                if (domain == blockedFreeEmail[i])
                {
                    SelectNotValidInput(info, field, clickd_MSG_FREE_EMAIL);
                    return false;
                }
            }
        }
    }
    return true;
}

Be First to Comment

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.