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 would begin troubleshooting by checking the value in the counter variable when the document is opened. Is it what you expect?
Copy link to clipboard
Copied
The counter variable resets to zero upon reopening of the document when it should be 5.
Copy link to clipboard
Copied
There's no need for a doc-level variable. Also, you should not reduce the value of the counter by 1 if the field is not ticked. It should only count the checked fields.
Copy link to clipboard
Copied
HI,
The code works for me without changes, although I believe you need to keep the counter -= 1, as this would be required if the user changes their mind, and therefore goes below the limited number of checkboxes.
Can I check a simple thing, how many checkboxes do you have? is it 101?
Regards
Malcolm
Copy link to clipboard
Copied
I do have more than 100 checkboxes. So I will up the limit. Thanks for pointing that out!
Copy link to clipboard
Copied
The problem is either I use the variable (counter) when value is zero or I use this.getField("Quantity_Produits_Finale").value in order to define that the limit number has been reached. But I don't know how to do it in code? There is where I need help.
Copy link to clipboard
Copied
I would do it like this (without any doc-level code):
function validateCheckBox(name,value){
var counter = 0;
var i=0;
var f = this.getField("chkBox" + i);
while (f) {
if (f.value == "Yes"){
counter += 1;
}
i++;
f = this.getField("chkBox" + i);
}
if (value == "Yes"){
counter += 1;
}
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)");
}
}
Copy link to clipboard
Copied
So if it is not a doc level Javascript where would you apply this... to the validation or action of each checkbox?
Copy link to clipboard
Copied
Check-boxes don't have a validation event. Use MouseUp, instead.
Copy link to clipboard
Copied
I tried using your code... but it doesn't seem to work. I can check as many boxes as I like without any message.
Copy link to clipboard
Copied
Also, you need to separate the check of the counter against the value of the maximum allowed number and the part that counts up the number of fields. The two things are not related and should not be a part of the same if-condition.
Copy link to clipboard
Copied
They are separate. //--------Count Checked boxes again when document is closed and re-opened ------------
+ //-------------Validation Function ---------------------------------------------------
Copy link to clipboard
Copied
HI,
Can you share the file, using a file sharing service, as I said earlier, your code with no changes works for me, I have a document with 101 check boxes, and I am unable to select more than 5. So I am not sure what the problem is.
Regards
Malcolm
Copy link to clipboard
Copied
Close the document. Then reopen. The 5 checkboxes now allows you to select 10. 5 from previous and now 5 more.
Copy link to clipboard
Copied
I want to stop at 5.
Copy link to clipboard
Copied
Here is the link to file Dropbox - Form_Kit_v20_FR_test.pdf
Copy link to clipboard
Copied
Why do you use check boxes with the same name?
Copy link to clipboard
Copied
Product is available for men and women and only counts as 1.
Copy link to clipboard
Copied
My code does work, IF:
- The fields in your file were actually named as what you said they were, which they are not (they are actually called "0 Check Box X", not "chkBox X").
- There were no gaps in the number part of the fields names, which there are (starting with 53, for example).
Copy link to clipboard
Copied
Hi try67!
Thank you for having a go at it. What I provided is my working file.
Not your implementation of it. As I don't quite understand how to make everything work according to your vision.
I know that there will be gaps in the field names as products are continuously added and removed. And the easiest way for me to do so is to add on numbers from last box rather than renaming each one every time there is a change.
Hope you can understand my reasoning behind this!
Copy link to clipboard
Copied
I don't, actually. If you do that it means you can't use a for-loop and will have to hard-code all the names into your code, or write a loop that iterates over all of the fields in the file, checking if they match a certain name pattern.
The code I provided can probably be adjusted for the latter, although it will require a more complex solution, but it's still much better than what you current have (and it works).
Copy link to clipboard
Copied
Does your solution account for check boxes that are the same name but different iteration? As they only count for one checked box. I will see if I can implement correctly.
Copy link to clipboard
Copied
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("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)");
}
}
Copy link to clipboard
Copied
I inserted your code. But I can't for some reason get it to work.