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,...
Suggested Answer

Portals-make fields mandatory Dynamically

(0) ShareShare
ReportReport
Posted on by

How to make fields mandatory conditionally(like based on some field value) with out using any code? Is this possible ?

I have the same question (0)
  • Suggested answer
    Fubar Profile Picture
    2,761 on at

    Not possible without adding code (unless you are in a Web Form and have a conditional step with 2 different forms one with the mandatory and the other without it).

    With JavaScript you add the 'required' class to the CSS of the element (to show the red asterix), then add a custom validator to put your logic for when it passes/fails the mandatory test.  The code below adds a custom validator to validate that the emailaddress has been populated if email has been specified as a preferred contact method (as per https://docs.microsoft.com/en-us/powerapps/maker/portals/configure/add-custom-javascript

     

    if (window.jQuery) {

      (function ($) {

         $(document).ready(function () {

            if (typeof (Page_Validators) == 'undefined') return;

            // Create new validator

            var newValidator = document.createElement('span');

            newValidator.style.display = "none";

            newValidator.id = "emailaddress1Validator";

            newValidator.controltovalidate = "emailaddress1";

            newValidator.errormessage = "<a href='#emailaddress1_label'>Email is a required field.</a>";

            newValidator.validationGroup = ""; // Set this if you have set ValidationGroup on the form

            newValidator.initialvalue = "";

            newValidator.evaluationfunction = function () {

               var contactMethod = $("#preferredcontactmethodcode").val();

               if (contactMethod != 2) return true; // check if contact method is not 'Email'.

               // only require email address if preferred contact method is email.

               var value = $("#emailaddress1").val();

               if (value == null || value == "") {

               return false;

               } else {

                  return true;

               }

            };

            // Add the new validator to the page validators array:

            Page_Validators.push(newValidator);

            // Wire-up the click event handler of the validation summary link

            $("a[href='#emailaddress1_label']").on("click", function () { scrollToAndFocus('emailaddress1_label','emailaddress1'); });

         });

      }(window.jQuery));

    }

  • Community Member Profile Picture
    on at

    Yes. I have used the same code but sometimes its not working user can able to proceed without providing the data.But its happening sometimes only.I am not  sure the reason.It might be the Cache issue?

  • Suggested answer
    Fubar Profile Picture
    2,761 on at

    Note, the above code is for an Entity form, there is slightly different code if using a Web Form step (the link in my previous post also has this).

    If its a CRM cache issue, clear the cache <your portal url>/_services/about , if its a Chrome cache issue F12 with the debugger open then right click on the refresh button on the browser (chrome will then give you a clear hard cache option.  (If you inspect the HTML in the debugger you can search for the code you updated to see if it has flowed through to the browser)

  • Community Member Profile Picture
    on at

    Thanks LA for your answer. I am using this code in Web-From Step of Edit Type. Which part of code that is different for entity form and web form step(I got confused here).

    Note: i included this code in function and am reusing this function as i am dealing with  more fields.

  • Suggested answer
    Fubar Profile Picture
    2,761 on at

    The trigger is different when the user presses the next/previous step buttons - Rather than using Page_validator it calls webFormClientValidate (you put your pass/fail code inside where the comments are in the example) see the following link

    docs.microsoft.com/.../add-custom-javascript

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 70 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

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

#3
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans