cancel
Showing results for 
Search instead for 
Did you mean: 
Reply
jamestredree
Frequent Visitor

Help with JAVA Field Validation

Hi

 

I need help with some Java code for tick box validation in Power Apps portal. I am not a coder.

 

This is brief description of the function of the code.

 

When the tab is presented, the 'next' button is locked until the following is met

2 sections on the form tab consist of - 1 bank of 5 tick boxes (allowed to select 2) and 1 bank of 3 tick boxes (Allowed to select 2)

If more than allowed selections are made, the system displays an error message.

 

While the below code works is does not add up the tick boxes as you proceed, it loops each section, so when you start the tab, the next button is locked, then when you complete the first bank of 2 tick boxes it unlocks the next button, and then when you select the first tick box in the 2nf bank it relocks locks the next button again until the 2nd bank of tick boxes is completed.

 

What I need to be able to do is not allow the next button to unlock until the total of 4 tick boxes has been selected, rather than looping 2x2.

 

I hope that makes sense. I think it's something in a highlighted count but I cant work it out.

 

Any help would be much appriciated

 

Thanks

 

Jim 

 

 

 

 

$(document).ready(function () {
$("#NextButton").prop("disabled", true);
})

$(document).ready(function () {
minimumNumberOfTickedBoxes = 4;
var section1 = ['tri_tickbox1', 'tri_tickbox2', 'tri_tickbox3', 'tri_tickbox4', 'tri_tickbox5']
var section1MaximumTickBoxNumber = 2;

var section2 = ['tri_tickbox6', 'tri_tickbox7', 'tri_tickbox8']
var section2MaximumTickBoxNumber = 2;

//Add the onChange event to every tickbox in the page. Elements can be copied, but the function still needs to be called.
$("#tri_tickbox1").change( function(event) {
validateNumberOfTickedBoxes(section1, section1MaximumTickBoxNumber, 2)
});
 
$("#tri_tickbox2").change( function(event) {
validateNumberOfTickedBoxes(section1, section1MaximumTickBoxNumber, 2)
});
 
$("#tri_tickbox3").change( function(event) {
validateNumberOfTickedBoxes(section1, section1MaximumTickBoxNumber, 2)
});
 
$("#tri_tickbox4").change( function(event) {
validateNumberOfTickedBoxes(section1, section1MaximumTickBoxNumber, 2)
});
 
$("#tri_tickbox5").change( function(event) {
validateNumberOfTickedBoxes(section1, section1MaximumTickBoxNumber, 2)
});

//Section 2 - Split for reading purposes
$("#tri_tickbox6").change( function(event) {
validateNumberOfTickedBoxes(section2, section2MaximumTickBoxNumber, 2)
});

$("#tri_tickbox7").change( function(event) {
validateNumberOfTickedBoxes(section2, section2MaximumTickBoxNumber, 2)
});
 
$("#tri_tickbox8").change( function(event) {
validateNumberOfTickedBoxes(section2, section2MaximumTickBoxNumber, 2)
});

});
//Parameters:
//section - The section that intends to be picked. Needs to be declared in the top function using the same schema.
//maximumNumberOfTickedBoxes - The maximum amount of tickboxes that the user plans to have selected in that section
//sectionNumber - To provide a detailed message on which section is the error
 
function validateNumberOfTickedBoxes(section, maximumNumberOfTickedBoxes, sectionNumber){
//Set the maximumNumber of ticked boxes here
var count = 0;
$.each(section, function(index, value){
if($("#" + value ).prop("checked") == true) {
count++;
}
});


if(count < maximumNumberOfTickedBoxes ){
$("#NextButton").prop("disabled", true);
}
else{
if(count > maximumNumberOfTickedBoxes ){
alert("Maximum number of tick boxes ticked for section " + sectionNumber);
$("#NextButton").prop("disabled", true);
}
else{
if(count == maximumNumberOfTickedBoxes){
if($("#NextButton").prop("disabled") == true){
$("#NextButton").prop("disabled", false);
}
if($("#PreviousButton").prop("disabled") == true){
$("#PreviousButton").prop("disabled", false);
}
}
}
}
}
0 REPLIES 0

Helpful resources

Announcements
UG GA Amplification 768x460.png

Launching new user group features

Learn how to create your own user groups today!

Community Connections 768x460.jpg

Community & How To Videos

Check out the new Power Platform Community Connections gallery!

Welcome Super Users.jpg

Super User Season 2

Congratulations, the new Super User Season 2 for 2021 has started!

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.

Users online (1,286)