Skip to main content

Notifications

Announcements

No record found.

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

Need to make a fields "required" setting conditional on a marketing form

Posted on by 28
Hello, I need assistance coding a field to make its /required/ setting conditional. 
 
I have not yet added the field to the form, but it will be a field called /company name/ and i want it to be visible all the time (which i can do), but required ONLY in the event that the event type = school & university OR corporate. 
 
Can someone please help me with this?
 
  • Suggested answer
    Dengliang Li Profile Picture
    Dengliang Li Microsoft Employee on at
    Need to make a fields "required" setting conditional on a marketing form
    Hi,
     
    It is possible to implement this functionality by inserting JavaScript into the Outbond marketing form.
        let EventType = document.getElementById("49ce3bed-4a59-ee11-be6f-002248213b02");
        let companyName = document.getElementById("81020a1e-4b59-ee11-be6f-002248213b02");
        let star = document.createElement('span');
        star.innerText="*";
        star.style.color = 'red';
        let label = document.getElementById("companyName");
        let index = EventType.selectedIndex;
        EventType.addEventListener("change",function(e){
            e.stopPropagation();
            let index = EventType.selectedIndex;
            companyName.removeAttribute('required');
            if(label.children.length !==0){
                label.removeChild(star);
            }
            if(EventType.options[index].innerText === "Corporate Event" || EventType.options[index].innerText === "School&University Events"){
            companyName.setAttribute('required',true);
            label.appendChild(star);
            }
        })
     
    1. The string in parentheses in this line of code comes from the id value of the /select/ label for the 'What Type of Event do you want to host?' field.
     
    2. The string in parentheses in this line of code comes from the id value of the /input/ label for the 'Company Name' field.
     
    3. The string in parentheses in this line of code comes from the id value of the /label/ label for the 'Company Name' field.
    This id value does not exist by default and needs to be added manually.
     
    4. The final result is shown below.
     
    I hope my answer is helpful to you! If you have any other questions, please feel free to contact me.
     
    Best Regards,
    Dengliang Li
     

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,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans