PGR - Validation before clicking next button | Experience Community
Skip to main content

PGR - Validation before clicking next button

  • March 25, 2026
  • 4 replies
  • 19 views

Forum|alt.badge.img+1

I have Pick Group and Rank question containing two groups. I have validation turned on with “each group contains min 1 max 1”. I want the group box to prevent adding any additional choices after 1. Currently, the group box allows more than one choice and validates when the user clicks next. I want the validation to occur when the user tries to add the second choice to the group. I want the attempted choice to bounce back to the list rather than allowing it to be added to the group box. 
Is this possible? 
 

4 replies

  • Level 4 ●●●●
  • March 26, 2026

Thats possible only using JS. 

 


Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • March 26, 2026

I tried a couple AI recommended javascripts but wasn’t successful. 


Forum|alt.badge.img+1
  • Author
  • Level 1 ●
  • March 26, 2026

!--scriptorstartfragment-->

Qualtrics.SurveyEngine.addOnload(function () {

  var $this = this;

 

  // Watch for any change in group selections

  jQuery('select.QPGRGroup').on('change', function () {

    var selectedGroup = jQuery(this).val();

 

    if (!selectedGroup) return;

 

    // Find other items assigned to the same group

    jQuery('select.QPGRGroup').not(this).each(function () {

      if (jQuery(this).val() === selectedGroup) {

        jQuery(this).val(''); // clear previous selection

      }

    });

  });

});
***!--scriptorendfragment-->

Above is the javascript i’m using but it’s not working. I can still add multiple choices to a single group. 


  • Level 4 ●●●●
  • March 27, 2026

The code below should work. Change the QID as per your Question ID.

 

Code ---

 

Qualtrics.SurveyEngine.addOnReady(function () {

    var SOURCE_ID = "#QID72items";
    var GROUP_IDS = ["#QID72group0", "#QID72group1"];

    function enforceOneItemPerGroup(groupEl) {
        if (groupEl._pgr_observer) return;

        var observer = new MutationObserver(function (mutations) {
            var hasAddition = mutations.some(function (m) {
                return m.type === "childList" && m.addedNodes.length > 0;
            });

            if (!hasAddition) return;

            // Target li items directly (excluding header/label rows)
            var items = jQuery(groupEl).find("li[role='listitem']");

            if (items.length > 1) {
                var oldItem = items.first();

                observer.disconnect();

                // Send old item back to the source list
                jQuery(SOURCE_ID).append(oldItem);

                setTimeout(function () {
                    observer.observe(groupEl, { childList: true, subtree: true });
                }, 100);
            }
        });

        observer.observe(groupEl, { childList: true, subtree: true });
        groupEl._pgr_observer = observer;
    }

    function init() {
        GROUP_IDS.forEach(function (selector) {
            var el = document.querySelector(selector);
            if (el) {
                enforceOneItemPerGroup(el);
            } else {
                var retries = 0;
                var interval = setInterval(function () {
                    var el = document.querySelector(selector);
                    if (el) {
                        enforceOneItemPerGroup(el);
                        clearInterval(interval);
                    }
                    if (++retries > 20) clearInterval(interval);
                }, 100);
            }
        });
    }

    init();
});