• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

How do I limit the number of radio buttons selected

New Here ,
May 09, 2017 May 09, 2017

Copy link to clipboard

Copied

Hi there!

I'm trying to insert some javascript into my form that limits the number of radio buttons. I have a total of 8 buttons that can be selected but I want to restrict only 3 or less to be selected (not all 8). I have inserted the javascript into the document – however something is going terribly wrong.

I have named each radio button "service". And added the following document level javascript:

function limitChoices() { 

 

    // Get the value of the radio button 

    var v = getField("service").value; 

 

    // Set the maximum number of check boxes that can be selected

    var max_cbs = v == 3; 

 

    // Initialize the selected check box count 

    var count = 0; 

 

    // Loop through the check boxes to see if the limit has been exceeded 

    for (var i = 0; i < 8; i += 1) { 

 

        // Increment the counter if the current check box is selected 

        if (getField("box." + i).value !== "Off") count += 1; 

 

        // If the count exceeds the limit, deselect the check box that was just selected 

        // and alert the user 

        if (count > max_cbs) { 

            event.target.value = "Off"; 

            app.alert("Only " + max_cbs + " colour(s) available for this option.", 3); 

            break; 

        } 

    } 

I have also added

limitChoices();

to each mouseover state of the radio button.

I have no real knowledge of javascript, so I realise the code I am using is probably wrong! Any help would be greatly appreciated

TOPICS
PDF forms

Views

2.0K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , May 10, 2017 May 10, 2017

It's not a good idea to use radio-buttons for this, as they can't be "un-selected". You should use check-boxes instead.

Votes

Translate

Translate
Community Expert ,
May 10, 2017 May 10, 2017

Copy link to clipboard

Copied

It's not a good idea to use radio-buttons for this, as they can't be "un-selected". You should use check-boxes instead.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
May 11, 2017 May 11, 2017

Copy link to clipboard

Copied

Thank you! That's where I was going wrong All solved and working!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Nov 19, 2020 Nov 19, 2020

Copy link to clipboard

Copied

LATEST
Hello, I have to create a vote among 25 buttons or check marks. People can only select 20 answers
how to create this limit of answers?
Script or? attached the pdf, we must select only 20 yes at most, thank you .

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines