Skip to main content
Community site session details

Community site session details

Session Id :
Power Pages - Power Apps Portals
Answered

Need help with JavaScript validation on combination of 2 fields

(0) ShareShare
ReportReport
Posted on by 48

I am trying to validate the combination of 2 fields with Javascript and this is probably a syntax error, but I've tried several variations with no luck. It gives the error on ALL combinations instead of just the two I need or doesn't work at all. I followed these articles an have attempted several things:

 

https://powerusers.microsoft.com/t5/Power-Apps-Portals/Show-Hide-Section-based-on-Yes-No-toggle-JavaScript/td-p/618918/page/2 

https://powerusers.microsoft.com/t5/Power-Apps-Portals/webform-javascript-validation/m-p/694546

https://powerusers.microsoft.com/t5/Power-Apps-Portals/Using-JavaScript-For-Web-Form-Validation-in-Power-Portal/m-p/681344 

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

 

One version:

 

 

 if (window.jQuery) {
 (function ($) {
 webFormClientValidate = function() {
 var account = $("#account").val();
 var question = $("#findcompany").val();
//need error message if company lookup is blank and answer is 'yes' OR if company lookup not blank and answer is 'no'. Yes/No is a 2 option set, so I've tried both true and false as well as 0 and 1 for the values, but same issue.
 if ((account == null && question == false) || (account != null && question == true) ) {
 return true
 } else {
 alert("Incorrect selection. If Company is selected and correct, select Yes. Otherwise if Company is not found and blank, select No.");
 return false;
 }
 };
 }(window.jQuery));
 }​

 

also tried this after finding the radio button post:

 

 if (window.jQuery) {
 (function ($) {
 webFormClientValidate = function() {
 var selectedValue = GetRadioSelectedValue($('#findcompany'));
 var account = $("#account").val();
 if ((account == null && selectedValue == 0) || (account != null && selectedValue == 1) ) {
 return true
 } else {
 alert("Incorrect selection description");
 return false;
 }
 };
 }(window.jQuery));
 }

GetRadioSelectedValue = function(input) {

 if (!!$(input).find("input[type=radio]")) {
 var controlName = $(input).find("input[type=radio]").first().attr("name");
 if (!!controlName) {
 return $("input[name='" + controlName + "']:checked").val();
 }
 }
 return "";
};

 

 

Still not working. Please help and thank you. 

 

  • Verified answer
    JEM123 Profile Picture
    48 on at
    Re: Need help with JavaScript validation on combination of 2 fields

    That worked for account! I feel like I tried that earlier 😂. I kept the same radio button logic I had in the second example of my post, and that is good. 

  • Verified answer
    OOlashyn Profile Picture
    3,496 Most Valuable Professional on at
    Re: Need help with JavaScript validation on combination of 2 fields

    Hi @JEM123,

    So lookup on portal are actually 3 input fields that contain main data - id of the record, entity name of the record and name part that you see as a text. When you query data field by id what you actually get is a text input that should contain id of the record. If it is empty it will not be null - instead it will be an empty text field ie "". 

    var account = $("#account").val();
    
    if(account == "") console.log("I AM EMPTY!");

    Regarding radio buttons - didn't use them in a while I much prefer checkboxes or dropdown lists. I will check it on my test portal and get back to you.

  • JEM123 Profile Picture
    48 on at
    Re: Need help with JavaScript validation on combination of 2 fields

    I'm not sure I understand. Account in this case is a lookup. If I need to check if a value is present, would I use null/not null? I found another way that uses $("#id").val().length - would that work? I tried, but I'm still getting the validation alert coming up incorrectly. 

  • natalya Profile Picture
    133 on at
    Re: Need help with JavaScript validation on combination of 2 fields

    Hi @JEM123 ,

    Try to use instead of NULL - the option value, in case of lookup field.

    if (( account =='915240000' && question == '915240000') ...

    It should work

    if you changed a style of the filed to horizontal/vertical option set then

    if ($('#yourfiled_1').is(':checked')==true)
     
     

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

Paul Stork – Community Spotlight

We are honored to recognize Paul Stork as our July 2025 Community…

Congratulations to the June Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

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

Leaderboard > Power Pages

#1
Lucas001 Profile Picture

Lucas001 60 Super User 2025 Season 1

#2
Fubar Profile Picture

Fubar 55 Super User 2025 Season 1

#3
surya narayanan Profile Picture

surya narayanan 35