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.
I've added a Javascript function onto the page and it is visible while inspecting the elements
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!
Hi,
Has your problem been solved?
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.
In my opinion, two possible causes of this error are code order and id fetch.
Please check it again.
Hi
Let met share my steps
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?
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.
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.
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)
André Arnaud de Cal...
291,969
Super User 2025 Season 1
Martin Dráb
230,842
Most Valuable Professional
nmaenpaa
101,156