Copy link to clipboard
Copied
I have a script that counts the checked boxes up to a predefined number (lets say 5) and stops users from checking more boxes. But when I close and reopen I can check another 5. Is there a way to prevent this from happening?
//----------------------------Document JavaScript Code ----------------------------
var counter = 0; // Checked counter
//--------Count Checked boxes again when document is closed and re-opened ------------
for (var i =0; i<= 100; i++){
if (getField("chkBox" + i).value == "Yes"){
counter += 1;
}
}
//-------------Validation Function ---------------------------------------------------
function validateCheckBox(name,value){
if (value == "Yes" && counter < this.getField("Quantity_Produits_Finale").value){
counter += 1;
}else if (value == "Off"){
counter -= 1;
}else{
getField(name).value = "Off";
app.alert("Vous ne pouvez pas sélectionner plus de " + this.getField("Quantity_Produits_Finale").value + " produit(s)");
}
}
//-----------------End of Document Javascript------------------------
Action on each checkbox
validateCheckBox(event.target.name,event.target.value);
Yes, it does. Here's the latest version of my code, it will work even with the gaps in your field names:
...function validateCheckBox(name, value){
var counter = 0;
for (var i=0; i<this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f==null) continue;
if (f.type=="checkbox" && /^0 Check Box/.test(f.name) && f.value=="Yes") {
counter++;
}
}
if (value == "Yes"){
counter++;
}
if (counter>Number(this.getField
Copy link to clipboard
Copied
I don't see where you inserted my code. It needs to come instead of
everything that you have at the doc-level. Everything else can stay the
same.
Copy link to clipboard
Copied
I got it! Thank you very much for your help!!!
Copy link to clipboard
Copied
Counter needs to start at -1 otherwise only 4 are selected and message pops at the 5th.
Copy link to clipboard
Copied
No, don't do that. Instead, change this line:
if (counter>Number(this.getField("Quantity_Produits_Finale").value)) {
To:
if (counter>=Number(this.getField("Quantity_Produits_Finale").value)) {
Copy link to clipboard
Copied
I reset counter value to 0. But I can only select 3 when adding the =
Copy link to clipboard
Copied
I found my mistake. Thanks!
Copy link to clipboard
Copied
You can actually remove this part of the code:
if (value == "Yes"){
counter++;
}
Copy link to clipboard
Copied
Even with >= and code removal counter needs set to -1. It is when user selects the 6th box that error message pops up.
Copy link to clipboard
Copied
Works fine for me...
function validateCheckBox(name,value){
var counter = 0;
for (var i=0; i<this.numFields; i++) {
var f = this.getField(this.getNthFieldName(i));
if (f==null) continue;
if (f.type=="checkbox" && /^0 Check Box/.test(f.name) && f.value=="Yes") {
counter++;
}
}
if (counter>=Number(this.getField("Quantity_Produits_Finale").value)) {
this.getField(name).value = "Off";
app.alert("Vous ne pouvez pas sélectionner plus de " + this.getField("Quantity_Produits_Finale").value + " produit(s)");
}
}
When clicking the fifth box, the error message appears.
Copy link to clipboard
Copied
That's it! I need to be able to check the fifth box. Hence the -1.
Copy link to clipboard
Copied
OK, then your description was not clear... Anyway, if it works leave it as is, although starting from -1 is not the best practice.
Copy link to clipboard
Copied
It works perfectly!
Copy link to clipboard
Copied
OK.
You should look at the console for error messages.
Copy link to clipboard
Copied
The issue I am having is the checked boxes counter reset to 0 every time I reopen this saved file and check new boxes. When I want it to retain the value of this.getField("Quantity_Produits_Finale").value (lets say 5) and stop people from checking new boxes.
So in other words on first go it stops at 5. OK
I save and reopen file
It allows me to check another 5 boxes when it should simply stop the user from checking more than 5 boxes NOT OK
I simply don't know how to implement that correctly?
Copy link to clipboard
Copied
Your code at file open is incorrect. Look at the error message.
Copy link to clipboard
Copied
Ok! How do I fix this?
Copy link to clipboard
Copied
Change the code.
Get ready! An upgraded Adobe Community experience is coming in January.
Learn more