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/webform-javascript-validation/m-p/694546
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.
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.
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.
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 ,
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
Lucas001
60
Super User 2025 Season 1
Fubar
55
Super User 2025 Season 1
surya narayanan
35