web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Adding fields validation using JavaScript to Marketing form in Dynamics 365

(0) ShareShare
ReportReport
Posted on by 69

Hi
I created a Marketing form that has email and confirms email addresses.
i added the Marketing form to a Marketing page (landing page).


When a user enters a different value for the "email" and "confirm email" addresses. it should give an error..


To achieve this:
I included the following code in the Marketing page html header.
I didn't get any error when i submit. Please advise me why the error was not displayed when i enter different values to email and confirm email.

<script type="text/javascript">
MsCrmMkt.MsCrmFormLoader.on('formSubmit', function(event) {
// sample validation - check if
document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').style.visibility = 'hidden';
var email = document.getElementById('7f685ebb-7c54-4cff-a1bc-772562d25c38').value;
var confirmEmail = document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').value;
if (email !== confirmEmail ) {
document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').style.visibility = 'visible';
event.preventDefault(); });

</script>
Fields in the marketing form:
<input class="lp-form-fieldInput form-control" id="48aa37a4-e4c2-ea11-a812-000d3a0c8127" name="48aa37a4-e4c2-ea11-a812-000d3a0c8127" placeholder="Enter your email" required="required" style="width:40%" title="" type="text">
<input class="lp-form-fieldInput form-control" id="7f685ebb-7c54-4cff-a1bc-772562d25c38" name="7f685ebb-7c54-4cff-a1bc-772562d25c38" placeholder="Enter your email" required="required" style="width:40%" title="" type="email">
Please advice me what i am missing to make the code work or any suggestion how i can add validation to marketing form fields. 
Thank you,

I have the same question (0)
  • cloflyMao Profile Picture
    25,210 on at

    Hi Siva,

    Please open F12 developer tool to check any error in console.

    I copied code your posted, it seems that a '}' symbol is missing thus the function(or the if statement) not close.

    Your original code:

    MsCrmMkt.MsCrmFormLoader.on('formSubmit', function (event) {
      // sample validation - check if
      document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').style.visibility = 'hidden';
      var email = document.getElementById('7f685ebb-7c54-4cff-a1bc-772562d25c38').value;
      var confirmEmail = document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').value;
      if (email !== confirmEmail) {
        document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').style.visibility = 'visible';
        event.preventDefault();
      }); 

    Error in console after I copied your code to my marketing page.

    pastedimage1594965657402v1.png

    Try to add a '}' in code.(I added an alert to detect whether submission is prevented.)

    MsCrmMkt.MsCrmFormLoader.on('formSubmit', function (event) {
      // sample validation - check if
      document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').style.visibility = 'hidden';
      var email = document.getElementById('7f685ebb-7c54-4cff-a1bc-772562d25c38').value;
      var confirmEmail = document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').value;
      if (email !== confirmEmail) {
        document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').style.visibility = 'visible';
        event.preventDefault();
        alert('Submission is not allowed.')
      }
    });

    Regards,

    Clofly

  • SivaR Profile Picture
    69 on at

    Thank you so much. It got the alert message.

    However, how i can display an error message as "Both email and confirm emails should be the same" something like that as attached image.

    validationError.GIF

  • cloflyMao Profile Picture
    25,210 on at

    Hi Siva,

    Unfortunately, the validation message is controlled by Dynamics Marketing, our custom validation message will be prevented by form-loader.js.

    You could instead append an custom element to Confirm Email field to show custom error message.

    8308.JPG

    MsCrmMkt.MsCrmFormLoader.on('formSubmit', function (event) {
      var element = document.getElementById('2670328b-2cbc-ea11-a812-000d3ab64f2b');
      var err = document.createElement("div");
      /** Optional, you can also predefine CSS in your marketing page.(By giving a class name to error message element.) */
      var style = "";
      style  = "border: 1px solid #F2F2F2; ";
      style  = "border-radius: 5px; ";
      style  = "background: #fff; ";
      style  = "color: red; ";
      style  = "padding: 3px; ";
      style  = "font-weight: 400; ";
      style  = "position: relative; ";
      style  = "z-index: 10000; ";
      style  = "bottom: 12px";
      err.setAttribute("style", style);
      element.parentNode.appendChild(err);
      if (element.value !== "sss@outlook.com") {
        event.preventDefault();
        err.innerText = 'Both email and confirm emails should be the same';
      } else {
        element.parentNode.lastChild.remove();
      }
    });

    We can define CSS in marketing page in advance to avoid long value of style variable in function.(By giving a class name to error message element.)

    Regards,

    Clofly

  • SivaR Profile Picture
    69 on at

    Thank you for the reply,

    I applied the code as below. however, somehow it stopped at the line "confirmEmail.parentNode.appendChild(err);"

    alert message after this line does not show up. Not sure why

    MsCrmMkt.MsCrmFormLoader.on('formSubmit', function (event) {
    // sample validation - check if
    var email = document.getElementById('7f685ebb-7c54-4cff-a1bc-772562d25c38').value;
    var confirmEmail = document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').value;

    var err = document.createElement("div");
    /** Optional, you can also predefine CSS in your marketing page.(By giving a class name to error message element.) */
    var style = "";
    style += "border: 1px solid #F2F2F2; ";
    style += "border-radius: 5px; ";
    style += "background: #fff; ";
    style += "color: red; ";
    style += "padding: 3px; ";
    style += "font-weight: 400; ";
    style += "position: relative; ";
    style += "z-index: 10000; ";
    style += "bottom: 12px";
    err.setAttribute("style", style);

    if (email !== confirmEmail) {
    event.preventDefault();
    alert(" before confirm===");
    confirmEmail.parentNode.appendChild(err);
    alert(confirmEmail + "====after confirmEmail");
    err.innerText = 'Both email and confirm email should be the same';
    } else {
    alert(confirmEmail + "====else confirmEmail");
    confirmEmail.parentNode.lastChild.remove();
    }

    });


    emailScreen.GIF
  • Verified answer
    cloflyMao Profile Picture
    25,210 on at

    Hi Siva,

    Because in your code, confirmEmail variable is equal to Confirm Email field value instead of the field node itself, therefor caused the error.

    -----------------------------------------------------------------------------------------------------------------------------------

    var confirmEmail = document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127').value;

    confirmEmail.parentNode.appendChild(err); -> confirmEmail here is test4@hotmail.com instead of Confirm Email field node object.

    -----------------------------------------------------------------------------------------------------------------------------------

    My suggestion is that you could remove value attribute to set the confirmEmail variable as a node object(1), then add value attribute back in if conditional statement(2).

    1. var confirmEmail = document.getElementById('48aa37a4-e4c2-ea11-a812-000d3a0c8127');

    2. if (email.value !== confirmEmail.value)

    Regards,

    Clofly

  • SivaR Profile Picture
    69 on at

    Thank you so much. i will try and let you know 

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 170 Super User 2025 Season 2

#2
#ManoVerse Profile Picture

#ManoVerse 61

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 52 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans