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

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

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

    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

  • Narendra Sunkara Profile Picture
    467 on at

    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

  • cloflyMao Profile Picture
    25,210 on at

    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

    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

  • Verified answer
    Justinjose Profile Picture
    2,707 on at

    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

  • cloflyMao Profile Picture
    25,210 on at

    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

  • Narendra Sunkara Profile Picture
    467 on at

    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

  • urklnme Profile Picture
    439 on at

    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.

  • urklnme Profile Picture
    439 on at

    please disregard previous post.

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

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

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

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

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans