Regular Expression for Text Validation (Regex)

Regular Expression for Text Validation (Regex)

 

Using regex (regular expressions) for validation in text-based assessments helps ensure that user inputs follow a specific format.

When a user enters something in a text field, the input is compared to the regex pattern. If the input matches the pattern, the validation is considered successful.

Administration

Regex can be configured for validation in the assessment settings of a text-based question type. To do this, activate the toggle labeled “Enable Regular Expression”.

image-20250709-091335.png

In the following example, we want to require a valid EAN code. An EAN (European Article Number) is a barcode format that typically consists of 13 digits.

To enforce this, we enter the following regex in the validation field:

^\d{13}$

image-20250709-091414.png

This expression means:

  • ^ – Start of the string

  • \d{13} – Exactly 13 digits (0–9)

  • $ – End of the string

When the assessment is run, the entered string is checked against the pattern, and the validation will pass or fail or succeed based on the input.

image-20250709-091541.png
image-20250709-091557.png

 

Examples

The following regex examples are commonly used in quality control scenarios:

1. Validate Serial Numbers (e.g., SN-DE-2025-000123)

Pattern:

2}-\d{4}-\d{6}$

Explanation:

  • Starts with SN-

  • Two uppercase letters (country code, e.g., DE, US, CN)

  • 4-digit year

  • 6-digit serial number

2. Expiration Date (Format: DD.MM.YYYY)

Pattern:

^(0[1-9]|[12][0-9]|3[01])\.(0[1-9]|1[0-2])\.(20\d{2})$

Explanation:

  • Day: 01–31

  • Month: 01–12

  • Year: 2000–2099

3. Batch Numbers with Format (e.g., CHR-20250708-A12)

Pattern:

^CHR-\d{8}-[A-Z]\d{2}$

Explanation:

  • Starts with CHR-

  • 8-digit production date (YYYYMMDD)

  • A dash, followed by one uppercase letter and two digits (e.g., line A, number 12)

4. Product Code with Allergen Indicator (e.g., MILK-FREE, NUT-FREE, EGG-CONTAINS)

^(MILK|NUT|EGG)-(FREE|CONTAINS)$

Explanation:

  • Allowed allergens: MILK, NUT, EGG

  • Indicators: FREE or CONTAINS

5. Inspector Initials and Date Combined (e.g., JD-08/07/2025)

Pattern:

^[A-Z]{2}-\d{2}/\d{2}/\d{4}$

Explanation:

  • Two uppercase letters (inspector initials)

  • Date in format DD/MM/YYYY

For further information and a comprehensive overview of regular expressions, visit regex101.com, where you can build, test, and analyze regex patterns step by step.

Alternatively, feel free to contact our Solution Team anytime with questions.