Skip to main content

Notifications

Power Pages - General Discussions
Unanswered

Make Form field on page required based on another fields value

(0) ShareShare
ReportReport
Posted on by 598

Hey All - brand new to java scripting in portal pages.

 

I'm trying to say if DOB Unknown is set to No, which is the default setting for this field, make Victims Date of Birth Required. If DOB unknown is set to Yes, don't make it required. I think I figured out the function (make changes to my fields from a snippet I found online 

 

//New code DOB required
function makeDobRequired() {
    debugger;
    var KnowDob = $("#hd_victim_dob_unknown").val();
    if (KnowDob  == 0) //if dob is  known
    {
        makeRequired('cra83_dateofbirth');
           }
    else if (KnowDob  == 1) ////if dob is not known
    {
        makeNotRequired('cra83_dateofbirth');
    }
}
 
but don't I have to call the function on the change event for the DOB unknown field? (#hd_victim_dob_unknown) > Field name. 
 
How do I do that? also - does it matter where I put the function in my code? I put it in the beginning after my
document).ready(function ()
. there is no other code in the script with these fields.  The Victims Date of Birth is set to optional in dataverse. 
 
thx 
 
ctedesco3307_0-1695060288839.png

 

Categories:
  • Lucas001 Profile Picture
    Lucas001 2,089 on at
    Re: Make Form field on page required based on another fields value

    Hi @ctedesco3307,

     

    as we are using a newer jQuery in Power Pages, could you follow that thread: https://stackoverflow.com/questions/6048022/jquery-attrdisabled-disabled-not-working-in-chrome

    and change the .attr to .prop?

     

    If the Id is not showing you can still use a class, easiest way is to use the chrome dev tools and copy the css selector. Test to change the .text() for example by executing jQuery inside the console.

  • ctedesco3307 Profile Picture
    ctedesco3307 598 on at
    Re: Make Form field on page required based on another fields value

    @Lucas001 if you look at my image I am in preview. The ID of the submit button is not visible. I did all the other thigs you mentioned already. 

  • Lucas001 Profile Picture
    Lucas001 2,089 on at
    Re: Make Form field on page required based on another fields value

    Hi @ctedesco3307,

     

    you will have to change all the ...your... jquery selectors, not only the one you mentioned.

    TO get the Ids and in some cases the css path, use the page in preview, not inside the portal management app. That way you will easily get your selectors.

     

    There were () missing in my code - I fixed it. In the future I propose using VS Code and pasting the code there - it mostly shows where the error occurs.

     

    Hope that helps.

    If you have any more problems, feel free to ask.

  • ctedesco3307 Profile Picture
    ctedesco3307 598 on at
    Re: Make Form field on page required based on another fields value

    @Lucas001 Thanks for the reply.  I'm not sure if I updated this correctly. Can you confirm I only need to put in the column name where you have 

    $(yourButtonDOBUnknown)

     

    etc? 

     

    Also - how do I find the control name for the submit button? This is OOB Power Pages and it seems really hard to find the id's here on an inspect ....  I don't know what the oob name is of the control. 

    ctedesco3307_1-1695234684051.png

     

     

     

    Here is my mods to your code.  Thx - also there does appear to be a missing bracket - where does it go? I put it on the end but it didn't like it 

     

    $(document).ready(function {
    //Executed synchronically from top to bottom
    $(crb09_DOBUnknown).on("click",function(){
    //Code to make your Field required.
    //Make it required by disabling your submit button.
    $(yourSubmitButton).attr("disabled",true)
    })
    $(crb09_DateofBirth).on("change",function() {
    //Check if input != null or != "" make buttom disabled false
    ($(crb09_DateofBirth).val() == "" || $(crb09_DateofBirth).val() == null) ? $(yourSubmitButton).attr("disabled",true) : $(yourSubmitButton).attr("disabled",true);
    })
    });


     

    ctedesco3307_0-1695234507082.png

     

     

     

  • Lucas001 Profile Picture
    Lucas001 2,089 on at
    Re: Make Form field on page required based on another fields value

    Hi @ctedesco3307,

     

    as you said the DoB column should be set to optional as it depends on another condition and will become required.

    I hope the code works as expected and I did not miss any bracket.

     

    The implementation should work when you use a similar code to:

     

     

    $(document).ready(function (){
    //Executed synchronically from top to bottom
    $(yourButtonDOBUnknown).on("click",function(){
     //Code to make your Field required.
     //Make it required by disabling your submit button.
     $(yourSubmitButton).attr("disabled",true)
    })
    $(yourDateValue).on("change",function() {
     //Check if input != null or != "" make buttom disabled false
     ($(yourDateValue).val() == "" || $(yourDateValue).val() == null) ? $(yourSubmitButton).attr("disabled",true) : $(yourSubmitButton).attr("disabled",true); 
    })
    })

     

     

     

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

    Hope that helps.

    If the post solves your problem, please mark it as solution. If it helps, consider a thumbs up.

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

Microsoft Kickstarter Events…

Register for Microsoft Kickstarter Events…

Announcing Our 2025 Season 1 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Announcing Forum Attachment Improvements!

We're excited to announce that attachments for replies in forums and improved…

Leaderboard

#1
WarrenBelz Profile Picture

WarrenBelz 145,666

#2
RandyHayes Profile Picture

RandyHayes 76,287

#3
Pstork1 Profile Picture

Pstork1 64,996

Leaderboard