web
You’re offline. This is a read only version of the page.
close
Skip to main content

Announcements

News and Announcements icon
Community site session details

Community site session details

Session Id :
Power Platform Community / Forums / Power Pages / Need help with JavaScr...
Power Pages
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. 

 

Categories:
I have the same question (0)
  • natalya Profile Picture
    133 on at

    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)
     
     
  • JEM123 Profile Picture
    48 on at

    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. 

  • Verified answer
    OOlashyn Profile Picture
    3,498 Most Valuable Professional on at

    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.

  • Verified answer
    JEM123 Profile Picture
    48 on at

    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. 

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

Introducing the 2026 Season 1 community Super Users

Congratulations to our 2026 Super Users!

Kudos to our 2025 Community Spotlight Honorees

Congratulations to our 2025 community superstars!

Congratulations to the March Top 10 Community Leaders!

These are the community rock stars!

Leaderboard > Power Pages

#1
rezarizvii Profile Picture

rezarizvii 53

#2
DP_Prabh Profile Picture

DP_Prabh 40

#3
oliver.rodrigues Profile Picture

oliver.rodrigues 28 Most Valuable Professional

Last 30 days Overall leaderboard