Highlighted

How do I limit the number of radio buttons selected

New Here ,
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

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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.

TOPICS
Macintosh, PDF forms

Views

774

Likes

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

How do I limit the number of radio buttons selected

New Here ,
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

Most Valuable Participant
Correct answer by try67 | Most Valuable Participant

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.

TOPICS
Macintosh, PDF forms

Views

775

Likes

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
May 09, 2017 0
Most Valuable Participant ,
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.

Likes

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
Reply
Loading...
May 10, 2017 0
New Here ,
May 11, 2017

Copy link to clipboard

Copied

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

Likes

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
Reply
Loading...
May 11, 2017 0
New Here ,
Nov 19, 2020

Copy link to clipboard

Copied

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 .

Likes

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
Reply
Loading...
Nov 19, 2020 0