Skip to main content

Notifications

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

Marketing page => adding javascript

(0) ShareShare
ReportReport
Posted on by 35

Hi there

I have a requirement where I need to add a check if a phone number has a certain length on the marketing page.

pastedimage1678363528721v3.png

I've added a Javascript function onto the page and it is visible while inspecting the elements

pastedimage1678363589100v4.png


MsCrmMkt.MsCrmFormLoader.on("afterFormLoad", function () {
document.getElementById("ac6a065d-364e-40d6-9a19-d9bf1ed4aa3e").addEventListener("change", function () {
var phone = document.getElementById("ac6a065d-364e-40d6-9a19-d9bf1ed4aa3e").value;
if (phone.value.length <= 2) {
alert("Phone must contain 3 or more digits");
}

})
});

The code doesn't seem to trigger anything. ElementID is correct.

Does anyone have a solution for this? Thanks!

  • Suggested answer
    Eiken Profile Picture
    Eiken on at
    RE: Marketing page => adding javascript

    Hi,

    Has your problem been solved?

  • Suggested answer
    Eiken Profile Picture
    Eiken on at
    RE: Marketing page => adding javascript

    Hi,

    It's strange, you've got all the steps right.

    I suggest you try to put the code in marketing form and test it again.

    pastedimage1678672124807v1.png

    In my opinion, two possible causes of this error are code order and id fetch.

    Please check it again.

    pastedimage1678672245432v2.png

  • J. Williams Profile Picture
    J. Williams 35 on at
    RE: Marketing page => adding javascript

    Hi

    Let met share my steps 

    1. Create a marketing form with the phone number on.
    2. Create a marketing page where I select the form.
      pastedimage1678449570363v2.png
    3. Add the script to the HTML designer
      pastedimage1678449704023v3.png
    4. Publish & go live
    5. I see the same error
      pastedimage1678449880148v4.png
    6. Looking back on the HTML the script shifted due to the tracking script base.
      pastedimage1678449925022v5.png
  • Suggested answer
    Eiken Profile Picture
    Eiken on at
    RE: Marketing page => adding javascript

    Hi,

    If the element ID is correct, the reason for the error is that the document is loaded from the top down. Using an unnamed variable will result in an error.

    So put your code at the end.

    Would you please show some screens about your code and the location of your code put?

  • J. Williams Profile Picture
    J. Williams 35 on at
    RE: Marketing page => adding javascript

    Thanks for your reply.

    Still not quite there yet I'm afraid.

    I've made the changes, but I do not get the same alert.

    I get this error when checking the script in the webpage.

    pastedimage1678437702950v1.png

        document.getElementById("ac6a065d-364e-40d6-9a19-d9bf1ed4aa3e").onblur=function ()

    When I inspect the element the ID is correct so not sure why the getElementByID is returning a null value.

  • Suggested answer
    Eiken Profile Picture
    Eiken on at
    RE: Marketing page => adding javascript

    Hi,

    You have used "value" twice in "var phone = document.getElementById("ac6a065d-364e-40d6-9a19-d9bf1ed4aa3e").value;
    if (phone.value.length <= 2) {".

    Now I edit you code for reference.(Remove a "value", replace the change with onblur)

    MsCrmMkt.MsCrmFormLoader.on("afterFormLoad", function () {
    document.getElementById("ac6a065d-364e-40d6-9a19-d9bf1ed4aa3e").onblur=function () {
        var phone = document.getElementById("ac6a065d-364e-40d6-9a19-d9bf1ed4aa3e");
        if (phone.value.length <= 2) {
        alert("Phone must contain 3 or more digits");
        }
        }
    })
    It runs well.
    pastedimage1678419150155v1.png

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

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Vahid Ghafarpour – Community Spotlight

We are excited to recognize Vahid Ghafarpour as our February 2025 Community…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,969 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 230,842 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans