Validation

Short response, long response, date, number, and multiple-choice fields support constraints — rules that a visitor's answer must satisfy before the form will let them submit.

To add one: open a question's More options, check Constrain, then Add constraint and pick a constraint type.

Constraint types

  • min / max — a numeric bound. For text fields this checks the length of the answer; for number fields it checks the value itself.
  • regex — a regular expression the answer must match. Enter it as pattern,flags (the part after the comma is optional), e.g. ^\d{3}-\d{3}-\d{4}$, for a US phone number.
  • func — a custom JavaScript validation function for anything the built-in types can't express.

While editing a constraint, its box turns green when the value you've typed is valid for that constraint type, red when it isn't, and yellow if a func constraint throws an error while being checked.

Writing a custom function (func)

A func constraint's body runs as the inside of a function that receives the answer as value and must return true (valid) or false (invalid). You only write the body — no function wrapper, no return needed if your last expression already evaluates to a boolean.

For example, to require the answer to be a non-empty string under 50 characters:

return value.trim().length > 0 && value.length < 50;

This runs as real, unsandboxed JavaScript in the visitor's browser (and is the reason this app's content-security-policy allows 'unsafe-eval'). Don't paste in untrusted code, and keep it to simple validation logic — it isn't a place to call out to other services or mutate the page.

See also: Field Types.