Skip to main content

Notifications

Community site session details

Community site session details

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

Setting required field conditionally in portal page

(0) ShareShare
ReportReport
Posted on by 467

Hi all,

I am using Custom portal in PowerApps portals

In my portal page I have radio buttons with two options(Yes and No)

I need to to set mandatory validation for few fields based on radio button selected value

How can I achieve this using JavaScript/JQuery

Thanks in advance

Regards,

Babu

  • urklnme Profile Picture
    439 on at
    RE: Setting required field conditionally in portal page

    please disregard previous post.

    cannot confirm that required field does not work when field is disabled

  • urklnme Profile Picture
    439 on at
    RE: Setting required field conditionally in portal page

    I used the metadata required setting with a required message for the field in question.

    In javascript/jquery I disable/enable the field based on a dropdown.

    when the field is disabled, the metadata validation/required is not done

    when the field is enabled, the metadata validation/required is done.

  • Narendra Sunkara Profile Picture
    467 on at
    RE: Setting required field conditionally in portal page

    Hi Clofly

    No worries and thank you for your effort.

    I could able to fix my problem by adding 4 newValidators as Justin told

    Regards,

    Babu

  • cloflyMao Profile Picture
    25,208 on at
    RE: Setting required field conditionally in portal page

    Hi Bahu,

    Sorry for keeping you waiting, it took nearly 2 hours to fix some issues of my portal.

    I have answered you in the new thread, please kindly check it. :)

    Regards,

    Clofly

  • Verified answer
    Justinjose Profile Picture
    2,707 on at
    RE: Setting required field conditionally in portal page

    Hi Babu,

    for 4 fields should create newValidator object same like before and push the object to  Page_Validators.push ( Page_Validators.push(newValidator))

    Thanks

    Justin Jose

  • Narendra Sunkara Profile Picture
    467 on at
    RE: Setting required field conditionally in portal page

    Hi Clofly,

    Thanks for your quick response. I will do that

    As I don't have much time, I raised a new thread

    Regards,

    Babu

  • cloflyMao Profile Picture
    25,208 on at
    RE: Setting required field conditionally in portal page

    Hi Babu,

    I'm trying to achieve your requirement in Potal, but currently my Portal is stuck at update, it needs some time to get updated and then research your new question.

    It seems that you created a new thread, could you kindly mark the current thread as verified to close it if my code would work for validation? It would be really appreciated.

    I will answer you in that new thread.

    Regards,

    Clofly

  • Narendra Sunkara Profile Picture
    467 on at
    RE: Setting required field conditionally in portal page

    Hi Clofly,

    Thanks for your response.

    With your code I could able to implement validation for one field.

    I need to set mandatory for 4 fields. How can I implement this

    Please help me

    Regards,

    Babu

  • Verified answer
    cloflyMao Profile Picture
    25,208 on at
    RE: Setting required field conditionally in portal page

    Hi Babu,

    Please take following code as reference:

    $(document).ready(function() {
    
    // Add event validator
    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 = "Email is a required field.";
    newValidator.evaluationfunction = function() {               
        if (!$('#new_acceptance_0').is(':checked') && $('#new_acceptance_1').is(':checked')) {
            if ($('#emailaddress1').val() == "") {
                return false;
            } else {
                return true;
            }
        } else {
            return true;
        }           
    };
    Page_Validators.push(newValidator);
    
    // Add validator element
    $("#new_acceptance_0").change(function() {
        if (this.checked) {
            $("#emailaddress1").attr('aria-required', "false");
            $('#emailaddress1').parent().prev().removeClass("required");
        }
    });
    
    $("#new_acceptance_1").change(function() {
        if (this.checked) {
            $("#emailaddress1").attr('aria-required', "true");
            $('#emailaddress1').parent().prev().addClass("required");
        }
    });
    
    })

    It consists of two part:

    1. Create a logic validator(newValidator variable) to prevent submit event and throw error message when specific condition meets.

    2. Create an element validator, if specific condition meets, add "required" class to parent label of field.

    e.g:

    If Acceptance field equals "Yes", then set Email field to be mandatory.

    1. By default:

    pastedimage1586426634165v1.png

    2. When "Yes" button is checked, add red "*" character to Email label.

    (If switch to "No", the element will be removed.) 

    pastedimage1586426667628v2.png

    3. Error message will appear if "Acceptance" equals "Yes" and Email field is empty.

    pastedimage1586426787906v3.png

    Core logic: return false if

    -> radio button 1 is not checked and radio button 2 is checked

    ->> if email field is emplty

    The article below introduced how to add custom validator.

    https://docs.microsoft.com/en-us/powerapps/maker/portals/configure/add-custom-javascript

    Regards,

    Clofly

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 the Engage with the Community forum!

This forum is your space to connect, share, and grow!

🌸 Community Spring Festival 2025 Challenge Winners! 🌸

Congratulations to all our community participants!

Adis Hodzic – Community Spotlight

We are honored to recognize Adis Hodzic as our May 2025 Community…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 225 Super User 2025 Season 1

#2
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 106 Most Valuable Professional

#3
Eugen Podkorytov Profile Picture

Eugen Podkorytov 102

Overall leaderboard

Product updates

Dynamics 365 release plans