javascriptautocompletejquery-ui-autocompletelimesurvey

Limesurvey autocomplete to disallow entry free text and show alert message


I have short free text question that would like participants to choose one answer from dropdown list. But the problem is there maybe some people that put free text or other not in the list.

So, is there any solution to ..

  1. disallowed free text or an answer that's not in the list can be submitted. (but still can type in)
  2. show alert message like "Not matched answer"

Below is what I am using now.

    $(document).ready(function() {
        $('#question{QID} input[type="text"]').autocomplete({
            minLength: 2,
            source: ["Wayne Rooney","Ryan Giggs","David Beckham","Christiano Ronaldo","Adison Cavani"]
        });
    });

It's just super simple autocomplete I think as I have very small knowledge about these type of thing (coding) . So, if you can keeps it simple that will be super cool. I did googled some but doesn't work at all maybe because I don't understand it.

Thanks in advance for any helpers.

Justin


Solution

  • You can use the autocomplete "change" event - https://api.jqueryui.com/autocomplete/#event-change

    $(document).ready(function() {
        $('#question{QID} input[type="text"]').autocomplete({
            minLength: 2,
            source: ["Wayne Rooney","Ryan Giggs","David Beckham","Christiano Ronaldo","Adison Cavani"],
            change: function( event, ui ) {
                if (!ui.item) {
                    alert('Invalid answer');
                    $('#question{QID} input[type="text"]').val('');
                }
            }
        });
    });