SurveyLoopr
Tips & Tricks
14 min read

XLSForm Character and Word Limits for Text Answers

Mehrab Ali
Mehrab AliPublished November 21, 2026

To set a character limit on a text answer in XLSForm, write string-length(.) <= 200 in the question's constraint column. For a word limit, count the spaces: string-length(normalize-space(.)) - string-length(translate(normalize-space(.), ' ', '')) <= 49 allows at most 50 words. ODK, KoboToolbox and SurveyCTO all read both rules. Every example below compiles with pyxform and ODK Validate, the check ODK Central runs when you upload a form.

Download example form (.xlsx)All eight examples in one XLSForm, one group each. Tested with pyxform and ODK Validate; free to reuse under CC BY 4.0.Download

How do I set a maximum number of characters?

Put string-length(.) <= N in the constraint column. Inside a constraint, . means "this question's answer". Here an enumerator describes a broken water point in 200 characters or fewer:

typenamelabelrequiredappearanceconstraintconstraint_message
textwp_problemDescribe the main problem with this water point.yesmultilinestring-length(.) <= 200Keep the description to 200 characters or fewer.
surveychoicessettings

Three things to know before you rely on it:

  • Everything counts. Letters, digits, punctuation, spaces and line breaks are each one character.
  • The form does not stop the typing. The enumerator can type past 200. The message appears when they try to move on, and they must shorten the answer first.
  • Say the number in the label or hint. A limit the enumerator only learns from an error message costs a retyped answer on every interview.

The multiline appearance gives a taller text box. It changes how the box looks, not how much it accepts.

How do I require a minimum length as well?

Join two checks with and. A minimum is the simplest way to stop "na", "." or a single word in a question where you need a real explanation, such as the reason a household refused the interview:

typenamelabelrequiredconstraintconstraint_message
textrefusal_reasonWhy did the household refuse the interview?yesstring-length(normalize-space(.)) >= 15 and string-length(normalize-space(.)) <= 300Write between 15 and 300 characters. One word is not enough.
surveychoicessettings

normalize-space() removes spaces at the start and end and squeezes repeated spaces into one before the count. Without it, "no" followed by thirteen spaces passes a 15-character minimum.

A minimum only bites when the question is answered. Set required to yes as well, or an empty answer skips the constraint. To clean the saved text itself, see uppercase and trimmed text in XLSForm.

How do I require an exact length or a range of lengths?

Use = for an exact length, or two bounds for a range. This suits codes printed on stickers, cards and registers, where a missing character means a record you cannot match later:

typenamelabelrequiredconstraintconstraint_message
textsticker_codeHousehold code on the door stickeryesstring-length(.) = 8The code has exactly 8 characters.
textregister_noClinic register numberyesstring-length(.) >= 6 and string-length(.) <= 10The register number has 6 to 10 characters.
surveychoicessettings

string-length() checks the length only. It accepts "????????" as an 8-character code. When the code has a fixed shape, such as two letters then six digits, use regex() instead; the phone number validation page shows digit patterns, and limiting digits and decimals covers number questions.

How do I set a word limit?

XLSForm has no word-count function. Count the spaces instead: a text of 50 words has 49 spaces between them. The rule below takes the length of the answer with single spaces, subtracts the length with all spaces removed, and checks that the difference is 49 or less:

typenamelabelrequiredappearanceconstraintconstraint_message
textcase_summarySummarise the case in 50 words or fewer.yesmultilinestring-length(normalize-space(.)) - string-length(translate(normalize-space(.), ' ', '')) <= 49The summary is over 50 words. Shorten it.
surveychoicessettings

Read it in three parts:

  • normalize-space(.) trims the answer and turns every run of spaces, tabs and line breaks into one space, so a double space is not counted as an extra word.
  • translate(..., ' ', '') deletes every space.
  • The difference between the two lengths is the number of spaces. Words = spaces + 1, so "at most 50 words" is "at most 49 spaces".

This counts the way a word processor does: anything between two spaces is one word, in any language. "2019/73/UWP" is one word, and so is "don't".

Warning:

The quotes inside translate() must be straight quotes with one space between the first pair: ' '. Word and some phone keyboards turn them into curly quotes, and the rule stops working. Type the constraint in the spreadsheet itself, or paste it as plain text.

How do I set a minimum and maximum number of words, and show the count?

Use the same space count with two bounds. For 10 to 100 words, the answer needs at least 9 and at most 99 spaces. A calculate row with the same expression plus 1 gives the word count, which a note can show back to the enumerator:

typenamelabelrequiredappearanceconstraintconstraint_messagecalculation
textchange_storyDescribe the most significant change in your household this year (10 to 100 words).yesmultilinestring-length(normalize-space(.)) - string-length(translate(normalize-space(.), ' ', '')) >= 9 and string-length(normalize-space(.)) - string-length(translate(normalize-space(.), ' ', '')) <= 99Write between 10 and 100 words.
calculatestory_wordsif(normalize-space(${change_story}) = '', 0, string-length(normalize-space(${change_story})) - string-length(translate(normalize-space(${change_story}), ' ', '')) + 1)
notestory_countWords written: ${story_words}
surveychoicessettings

The if() is there for the empty answer: no text has zero spaces, and zero plus 1 would report one word. The if statement guide explains the three arguments.

The count is not live. A calculate updates when the enumerator leaves the text box, not on each keystroke, so the note shows the count of the answer as it was last saved.

How do I show how many characters are left?

Subtract the length from the limit in a calculate, then show it in a note. This example is a reminder message that has to fit in one 160-character SMS:

typenamelabelrequiredconstraintconstraint_messagecalculation
textsms_textReminder message for the caregiver (160 characters at most)yesstring-length(.) <= 160The message is longer than 160 characters.
calculatesms_left160 - string-length(${sms_text})
notesms_left_noteCharacters left: ${sms_left}
surveychoicessettings

As with the word count, the number updates after the enumerator leaves the field. A negative number means the answer is over the limit by that many characters.

Should I use regex() for a word limit?

You will often see this pattern, which allows up to 30 words:

typenamelabelrequiredconstraintconstraint_message
textfeedback_enFeedback in English (30 words at most)yesregex(., '^\W*(\w+\b\W*){0,30}$')Write 30 words or fewer.
surveychoicessettings

It compiles and it works for plain English text. It has three limits you should know about:

  • \w only knows the letters a to z, digits and the underscore. An answer typed in Bangla, Arabic, Amharic, Hindi or any other non-Latin script contains no \w characters at all, so the pattern matches with zero words and the limit never applies.
  • Punctuation inside a word splits it. "don't" counts as two words and "2019/73/UWP" as three, so the form rejects answers that a word processor says are under the limit.
  • Long answers are slow. Patterns with a repeat inside a repeat can take a long time on a few hundred words, and small edits to the pattern make it worse.

The space count has none of these problems. Use regex() when you need to check what the characters are, not how many words there are.

Can the form shorten a long answer by itself?

Not the answer itself, but a calculate can keep a shortened copy. substr(text, start, end) returns the characters from position start up to, not including, position end, counting from 0:

typenamelabelrequiredappearancecalculation
textincident_detailDescribe the incident in full.yesmultiline
calculateincident_shortsubstr(${incident_detail}, 0, 80)
noteincident_previewShown on the summary sheet: ${incident_short}
surveychoicessettings

Both columns are in the export: the full text and the first 80 characters. This is useful for a summary table or a label, where long text breaks the layout. Do not use it in place of a constraint: silently cutting an answer loses information the enumerator thought was saved.

Common length-limit mistakes and how to fix them

The messages in the third column are what pyxform and ODK Validate return, which is what you see when ODK Central, KoboToolbox or the XLSForm converter rejects a form:

You wroteWrite insteadError you will see
string-length(.) <= 200 charactersstring-length(.) <= 200Couldn't understand the expression starting at this point
string-length(.) =< 200string-length(.) <= 200contains invalid constraint expression [string-length(.) =< 200] Bad node
string-length(.) >= 15 AND string-length(.) <= 300Lowercase andCouldn't understand the expression starting at this point
string-length(.) <= 200)Remove the extra bracketUnbalanced brackets or parentheses!
string-length(${wp_problm}) <= 200Fix the name, or use .Could not find the name 'wp_problm'
len(${sms_text}) in a calculatestring-length(${sms_text})cannot handle function 'len'
word-count(${story}) in a calculateThe space count abovecannot handle function 'word-count'
len(.) <= 200 in a constraintstring-length(.) <= 200No error at upload. Constraints are not run until someone answers, so test the question in preview
. <= 200 on a text questionstring-length(.) <= 200No error, but it compares the answer itself with 200, not its length
regex(., '^.{1,200}$')string-length(.) <= 200No error, but . does not match a line break, so every multi-line answer is rejected

The last three rows upload cleanly and fail in the field, so they are the ones to test for. For other messages, see common XLSForm errors, or paste your form into the free XLSForm validator.

Note:

A "character" is not always what the eye sees. In Bangla, Hindi and other scripts that attach vowel signs to letters, each sign is counted separately, and many emoji count as two or more. Test your limit with real text in the language your team types in before you fix the number.

Does this work the same in ODK, KoboToolbox and SurveyCTO?

Yes. string-length(), normalize-space(), translate(), substr() and regex() are part of the XPath function set all three platforms use, and the constraints on this page move between them unchanged. What differs is where you type them:

PlatformWhere the rule goesNotes
ODK (XLSForm)constraint and constraint_message columnsChecked in Collect when the enumerator moves forward, and in web forms when they leave the field
KoboToolbox FormbuilderQuestion settings, Validation Criteria, entered manually as XLSForm codeThe error text goes in the Error Message box
KoboToolbox (XLSForm upload)Same as ODKKobo's own help article uses the regex() word pattern; the space count works there too
SurveyCTOconstraint and constraint message columnsSame functions. The message column name has a space, not an underscore

None of the three apps stops the typing at the limit. All of them show your message and hold the answer until it is fixed.

Why set a length limit at all?

Because something downstream has one. An SMS carries 160 characters. A printed referral slip has a fixed box. An Excel cell holds 32,767 characters, and a donor template may give you 100 words per case story. If the form accepts more than the next step can take, somebody shortens the text later, without the respondent in front of them.

A minimum length protects quality the other way. Open-ended questions near the end of a long interview collect "ok" and "nothing" unless the form asks for more. Fifteen characters is a low bar, and it is enough to stop the one-word answer.

Do not set limits you do not need. A short cap on a qualitative question cuts off the detail you asked for.

Tip:

You can describe the rule in plain language instead: "the case summary must be 10 to 100 words, and show the word count below it". LooprAI writes the constraint, the calculate and the message, then compiles the form with pyxform before anything is saved, so a curly quote or a missing bracket never reaches your form.

Build my first form free

Next: XLSForm constraint examples →

Frequently Asked Questions

How do I limit the number of characters in an XLSForm text question?

Write string-length(.) <= 200 in the constraint column of the text question, and put the limit in the constraint_message. The dot is the answer being checked. Spaces, punctuation and line breaks all count as characters, in ODK, KoboToolbox and SurveyCTO alike.

How do I set a word limit in XLSForm?

There is no word-count function, so count spaces. Subtract the length of the answer with spaces removed, using translate(), from its length after normalize-space(). Fifty words have 49 spaces, so the difference must be 49 or less. The full constraint is on this page.

How do I require a minimum length for a text answer?

Use string-length(normalize-space(.)) >= 15 in the constraint, and set required to yes. normalize-space() removes padding spaces before the count, so an answer cannot reach the minimum with blanks. Join it to a maximum with and when you need both bounds.

Why does the regex word limit not work for my language?

The common pattern counts words with the regex class for word characters, which only covers the letters a to z, digits and the underscore. Text in Bangla, Arabic, Hindi or Amharic contains none of them, so the pattern finds zero words and always passes. Count spaces instead.

Does ODK Collect stop typing when the character limit is reached?

No. A constraint is checked after the answer is entered, not on each keystroke. The enumerator can type past the limit, then sees your constraint_message when moving to the next question and must shorten the answer. State the limit in the label so it is known first.

Can I show a live word or character count in the form?

You can show a count, but not a live one. Add a calculate row with string-length() of the answer, or the space count plus 1 for words, and reference it in a note. The number updates when the enumerator leaves the text box, not while they type.

No Credit Card Required

Build the form first. Add hosting when you need it.

Start free with LooprAI, then deploy a managed ODK Central server or run DataSnap checks when your project is ready.

Automation handles the mechanical work.

Researchers decide what the evidence means.