Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

Error Message in Marketing Form

Posted on by 235

Hello everyone,

i have a Marketing form and i need to change the display error message see Picture.

my form : 2020_2D00_12_2D00_23-16_5F00_28_5F00_21_2D00_Newsletter-abonnieren-_5F00_-Digital_2D00_Kompass.png

and i want That my error message be like This:2020_2D00_12_2D00_23-16_5F00_28_5F00_59_2D00_Kontakt-_5F00_-Digital_2D00_Kompass.png

here is my Code

Test Newsletter

Abonnieren Sie unseren
Test Newsletter!

Nach der Übermittlung Ihrer Kontaktdaten erhalten Sie von info@test.de einen Bestätigungslink, um die Anmeldung abzuschließen.

* Pflichtfelder

*
*
 
 

  • Suggested answer
    cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Error Message in Marketing Form

    Hi hedi,

    It seems that the form tag is already placed in source code, if so, please change code inside the script tag to the following:

    const form = document.getElementsByTagName("form")[0];
    
    const errorDiv = document.createElement("div");
    errorDiv.classList.add("error-div");
    
    const closeButton = document.createElement("span");
    closeButton.setAttribute("id", "close");
    closeButton.innerText = "x";
    errorDiv.appendChild(closeButton);
    
    form.parentNode.insertBefore(errorDiv, form);
    
    document.addEventListener("click", function (e) {
        if (e.target && e.target.id == "close") {
            errorDiv.classList.remove("active");
        }
    });
    
    addEvent(form, "submit", function () {
        const requiredFields = document.getElementsByClassName("is-required");
    
        const isValid = [];
    
        for (let i = 0; i < requiredFields.length; i  ) {
            if (requiredFields[i].value === "") {
                if (requiredFields[i].parentNode.querySelector("label") !== null) {
                    var fieldName = requiredFields[i].parentNode.querySelector("label").innerText;
                    addErrorMessage(i, fieldName);
                    isValid[i] = false;
                }
            } else {
                clearErrorMessage(i);
                isValid[i] = true;
            }
        }
    
        if (isValid.every((bool) => bool) === false) {
            errorDiv.classList.add("active");
            return false;
        } else {
            errorDiv.classList.remove("active");
            // Pass validation
            return true;
        }
    })
    
    function addErrorMessage(index, name) {
        const errorMsg = document.getElementById("error-"   index);
        if (errorMsg === null) {
            errorDiv.innerHTML  = "

    " name "is required.

    "; } } function clearErrorMessage(index) { const errorMsg = document.getElementById("error-" index); if (errorMsg !== null) { errorMsg.remove(); } } function addEvent(element, event, callback) { let previousEventCallBack = element["on" event]; element["on" event] = function (e) { const output = callback(e); if (output === false) return false; if (typeof previousEventCallBack === "function") { output = previousEventCallBack(e); if (output === false) return false; } }; }

    Regards,

    Clofly

  • Hedi bn Profile Picture
    Hedi bn 235 on at
    RE: Error Message in Marketing Form

    Hi cloflyMao Thank u very much for your answer.

    i tried to do your Solution but it still not working can u give me mor Info

    I Modify my Code like this i add the class is-required j´for the Email Text.

    
        
    
    
    
    Digital-Kompass-Newsletter

    Abonnieren Sie unseren
    Digital-Kompass Newsletter!

    Nach der Übermittlung Ihrer Kontaktdaten erhalten Sie von info@digital-kompass.de einen Bestätigungslink, um die Anmeldung abzuschließen.

    * Pflichtfelder

    *
    *
     
     

     

  • cloflyMao Profile Picture
    cloflyMao 25,198 on at
    RE: Error Message in Marketing Form

    Hi Hedi,

    We need to write code to achieve custom form validator, here is my sample based on your description and requirement.

    1. In source code of marketing form, add "is-required" as extra class name to input elements that you want to apply custom validator with.  

    7673.a1.JPG

    2. Add following code to source code of corresponding marketing page.(You can put it after end tag of body.)

    Test

    (I add custom validator to Last Name and Email)

    1: Leave both of fields value empty

    pastedimage1608796896791v1.png

    2 Leave Email field empty

    pastedimage1608797018335v2.png

    3. Leave Last Name empty

    pastedimage1608797055478v3.png

    Submission will success if both of required fields are filled with data.

    Reference:

    Extend marketing forms using code (Dynamics 365 Marketing Developer Guide) | Microsoft Docs

    Client-side form validation - Learn web development | MDN (mozilla.org)

    Regards,

    Clofly

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,235 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans