Expand my Community achievements bar.

Looping Through Form Validating Checkbox Options

Avatar

Level 2

We would like to add a validation through our form before other scripts get implemented. Here's the workflow:

  1. User goes through and provides answers to checkbox radio buttons (user can only selection one of 3 options)
  2. The user hits a button and there is a validation that the user answered all of the questions.
  3. If the user did not answer all the questions, the form displays the questions the users did not answer in one alert box (Lists the questions by number, preferably).

I found the following script on another website, and while it looks right, I assume that I don't know how to appropriately pull first all the objects in my document and then only pull the radio buttons for the check.

function validateForm()

{

    var ShowAlert = '';

    var AllFormElements = xfa.host.topmostSubform.getElementById("FormID").elements;

    for (i = 0; i < AllFormElements.length; i++)

    {

        if (AllFormElements[i].type == 'radio')

        {

            var ThisRadio = AllFormElements[i].name;

            var ThisChecked = 'No';

            var AllRadioOptions = document.getElementsByName(ThisRadio);

            for (x = 0; x < AllRadioOptions.length; x++)

            {

                 if (AllRadioOptions[x].checked && ThisChecked == 'No')

                 {

                     ThisChecked = 'Yes';

                     break;

                 }

            }  

            var AlreadySearched = ShowAlert.indexOf(ThisRadio);

            if (ThisChecked == 'No' && AlreadySearched == -1)

            {

            ShowAlert = ShowAlert + ThisRadio + ' radio button must be answered\n';

            }    

        }

    }

    if (ShowAlert != '')

    {

    alert(ShowAlert);

    return false;

    }

    else

    {

    return true;

    }

}

I'm not particularly attached to this scripting, so if you have a better way of completing this please feel free to let me know.

Thank you in advance for your assistance.

0 Replies