cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
JEM123
Helper I
Helper I

Need help with JavaScript validation on combination of 2 fields

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-Java... 

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-P... 

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. 

 

2 ACCEPTED SOLUTIONS

Accepted Solutions

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.

----------------------------------------------------
If you find this post helpful consider marking it as a solution to help others find it.

View solution in original post

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. 

View solution in original post

4 REPLIES 4
NatGeo
Helper I
Helper I

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)
 
 

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. 

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.

----------------------------------------------------
If you find this post helpful consider marking it as a solution to help others find it.

View solution in original post

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. 

View solution in original post

Helpful resources

Announcements
PA_User Group Leader_768x460.jpg

Manage your user group events

Check out the News & Announcements to learn more.

Power Query PA Forum 768x460.png

Check it out!

Did you know that you can visit the Power Query Forum in Power BI and now Power Apps

Carousel 2021 Release Wave 2 Plan 768x460.jpg

2021 Release Wave 2 Plan

Power Platform release plan for the 2021 release wave 2 describes all new features releasing from October 2021 through March 2022.

R2 (Green) 768 x 460px.png

Microsoft Dynamics 365 & Power Platform User Professionals

DynamicsCon is a FREE, 4 half-day virtual learning experience for 11,000+ Microsoft Business Application users and professionals.

Users online (2,247)