Validation: make sure the answers you get are usable

Validation: make sure the answers you get are usable

Validation stops participants submitting an answer that cannot be right — a percentage over 100, an email address with no @, a budget split that does not add up. It is worth the effort: an answer you reject at the time is one you do not have to clean up, or throw away, after fieldwork.

LimeSurvey gives you three levels, and you should reach for them in this order:

  1. Mandatory — must this be answered at all?
  2. Built-in limits — the ready-made attributes on the question, such as a character limit.
  3. Validation equations — ExpressionScript, for any rule the built-in options cannot express.

Mandatory: On, Soft, or Off

Open a question, and in the Question settings panel choose the Advanced tab. Mandatory has three settings:

  • On — participants cannot continue without answering.
  • Soft — participants are prompted if they leave it blank, but may continue anyway. Good for questions you want but cannot demand.
  • Off — answering is optional.

Soft is worth knowing about: it lets you press for an answer without hard-blocking someone who genuinely cannot give one.

Built-in limits

The Advanced tab shows the attributes that apply to the question type you are editing, so what you see changes with the type. A question that takes typed text — an Array (Texts), for example — offers Max characters. Set it and the participant simply cannot type more.

The classic question editor groups the logic-related attributes under its Logic section, including:

  • Minimum answers / Maximum answers — how many boxes may be ticked, for multiple choice and arrays. This is the usual way to enforce "pick exactly three".
  • Exclusive option — the answer code that, when selected, clears the others. This is what makes a "None of the above" option behave properly.
  • Question validation equation and Question validation tip — described below.

Always try these before writing an equation: they are quicker, and a colleague can understand them at a glance.

Validation equations

A question validation equation is an ExpressionScript expression that must be true before the answer is accepted. It is the escape hatch for any rule the ready-made attributes cannot express.

The field lives in the classic question editor, under LogicQuestion validation equation — so this is one of the settings you step out of the new editor to reach.

Refer to questions by their question code — whatever you set as the code, such as q1 or part1 — and to a sub-question as the question code, an underscore, and the sub-question code, as in q1_SQ001.

A number must be between 1 and 100:

q1.NAOK >= 1 && q1.NAOK <= 100

Three amounts must not exceed a budget (here the sub-questions of Test are A_1, A_2 and A_3):

sum(Test_A_1.NAOK, Test_A_2.NAOK, Test_A_3.NAOK) <= 1000

Two figures must add up to exactly 100:

(part1.NAOK + part2.NAOK) == 100

Only require an email address when a name was given:

is_empty(name.NAOK) || !is_empty(email.NAOK)

Note that ExpressionScript has no comment syntax — there is no //. Everything in the field is part of the expression, so do not paste explanatory notes alongside it.

Two syntax rules save most of the debugging:

  • No curly braces. In question attributes such as a validation equation, the field is already treated as an expression. Braces are only for tailoring — putting a value into text.
  • Use .NAOK on values you reference. It means "not applicable is OK", so the expression still evaluates when a question was skipped or hidden. Without it, one unanswered question can make the whole equation fail silently.

Useful functions include sum(), count() (how many were answered), is_empty(), and if(). The editor highlights unknown variable names and function errors in red, which is the fastest way to catch a typo.

Always write a validation tip

An equation only tells LimeSurvey what is wrong. The Question validation tip tells the participant — and without it they see a rejection with no explanation and abandon the survey.

Write the rule, not the error: "Please enter a number between 1 and 100" or "The three amounts must add up to 1000 or less." The tip is shown with the question and highlighted when validation fails.

Test every rule before you activate

Validation is the easiest thing in a survey to get subtly wrong, and the damage only shows once real people are stuck. Open the preview (the eye icon) and deliberately try to break each rule: enter a value that should pass, then one that should fail, and confirm the tip that appears actually explains what to do.

Pay particular attention to skipped questions: check that your rule does not block someone who legitimately left an earlier question blank. That is the most common validation bug, and .NAOK is usually the fix.

Related articles

    • Related Articles

    • Question and answer codes

      What codes are Every question, sub-question, and answer option in LimeSurvey has a short identifier — its code: Question codes — one per question, such as Q001. LimeSurvey assigns these automatically; you can rename them. Sub-question codes — one per ...
    • Use answers from previous questions (piping)

      Piping means inserting a participant's earlier answer into the text of a later question — for example, greeting them by name or referring back to a choice they made. In LimeSurvey, you do this by giving the earlier question a question code and then ...
    • Reorder, preview, and manage questions

      Once your questions exist, most day-to-day work happens in the survey editor: changing the running order, previewing the result, reviewing a question's setup, and reusing earlier answers in later questions. This article covers those tasks. For ...
    • Conditions: show a question only when it applies

      A condition makes a question appear only when it is relevant. Ask "Which brand do you drive?" only of people who said they own a car, or show a follow-up only to people who rated you poorly. Two things are worth understanding before you start: ...
    • Add and Edit a Question

      Questions are the heart of your survey. In the survey editor, questions sit inside question groups on the canvas — the large working area in the middle of the screen. Every new survey starts with a sample group and question, so you can begin right ...