# Validation rules

URL: https://docs.sparklayer.io/help/forms/validation

Check answers in SparkLayer Forms before they're submitted, with built-in format checks and custom rules using text, number, length and cross-field conditions.

> **Quick summary**
>
> - Every field gets built-in checks for its type, such as a valid email address. To add your own business rules, add custom validation rules, each with an error message.
> - You add rules per field on the **Validation** tab of **Configure field** in the form builder, at **Forms** in the SparkLayer Dashboard ([open](https://app.sparklayer.io/forms)). Forms is only in the SparkLayer Dashboard, not in the SparkLayer Wholesale app in Shopify admin.
> - A rule's condition describes the answers you **allow**: the error shows when the condition is false.
> - Validation runs while the form is filled in and whenever an entry is edited. A form can't be submitted while any rule fails.

## How it works

Validation stops incorrect data reaching your team, workflows and store. It works in layers, and the first two need no code:

Layers of validation:

1. **Built-in checks**: Format checks for each field type, with no setup.
2. **Custom rules**: Your business rules, such as a minimum length or matching values.
3. **Workflows**: Advanced checks, such as against an external system.

### Built-in checks

Each field type checks its own format, with no setup:

| Field type | Built-in check |
| --- | --- |
| **Email** | A valid email structure. |
| **URL** | A valid web address. |
| **Number** | A number within range. |
| **Date** | A valid date. |
| **Phone** | Valid digits. |
| **File upload** | File type and size limits. |

To make a field mandatory, tick **Required** on the field's **Properties** tab.

## Add a validation rule

Add a custom rule when the built-in checks aren't enough, for example to require a minimum length.

1. In the form builder, select the field.
2. In **Configure field**, open the **Validation** tab.
3. Click **Add rule**.
4. Set the **Condition**: the values you want to allow.
5. Enter the **Error message** customers see when the condition isn't met.

Changes save to your draft automatically.

> **Describe what's allowed, not what's blocked**
>
> A rule's error message shows when its condition is false, not when it matches. Say you don't ship to the United Kingdom. Don't set the condition to "country equals United Kingdom": that shows the error for every other country. Instead, set it to the countries you do ship to, such as "country is one of France, Germany, Spain". The error then only shows for a country outside that list.

## Types of rule

A rule can check a field on its own, or against other data:

| Type | What it checks | Examples |
| --- | --- | --- |
| **Simple value rules** | A field's own answer. | "Must be at least 10 characters", for password strength, reference numbers and minimum input. |
| **Cross-field and data reference rules** | An answer against another field, or against other data through [data references](https://docs.sparklayer.io/help/forms/data-references.md). | The confirmation email matches the email; the end date is after the start date; a purchase ID matches one of the customer's SparkLayer purchases. |

## Operators

Validation uses the same operators as [conditional logic](https://docs.sparklayer.io/help/forms/conditional-logic.md). Which ones you can choose depends on the type of field.

**Every operator**

| Operator | Example use |
| --- | --- |
| **Matches pattern** | A regular expression (regex) format check. |
| **Contains** | Must include a value. |
| **Starts with** | A required prefix. |
| **Ends with** | A required suffix. |
| **Greater than / equal** | A minimum quantity. |
| **Less than / equal** | A maximum value. |
| **Equals / not equals** | An exact match. |
| **Minimum length** | Password rules. |
| **Maximum length** | Reference codes. |
| **Exact length** | Postcodes or IDs. |
| **Is email** | An email format check. |
| **Is number** | A numeric value. |
| **Is integer** | A whole number. |
| **Is alpha / alphanumeric** | Letters only, or letters and numbers. |

Common patterns include making sure an end date is after a start date, and preventing duplicate items in a repeatable group or multi-select field.

## Write error messages

A good error message says what's wrong and how to fix it, in plain words. For example, write "Phone number must include area code", not "Invalid input". Messages can include values through templates, such as the required minimum.

## Validation states

A form can only be submitted when every rule passes.

**The three validation states**

| State | What it means |
| --- | --- |
| **Valid** | All rules pass. The form can be submitted. |
| **Invalid** | One or more rules fail. The form can't be submitted. |
| **Pending** | A check is still running. The form can't be submitted yet. |

Before a customer interacts with a field, it shows no indicator. After that, it shows an error message when invalid, and optionally a success indicator.

## Validate with workflows

For checks the form can't do itself, use [workflows](https://docs.sparklayer.io/help/forms/workflows.md). A workflow can run on submission, a page change or an answer update. It can run advanced checks, validate against external systems and apply logic across forms. Inside a workflow, **Condition** steps can update answers, notify your team about invalid data or start follow-up actions.

## Tips

- Layer your validation: built-in checks for format, custom rules for business logic, and workflows for advanced checks.
- Validate early rather than only on submit, show clear instructions, and avoid overly strict rules.
- Test empty values, the minimum and maximum, special characters and very long answers.
- Keep forms fast: avoid heavy regular expressions and complex cross-field rules where you can, and use workflows for advanced processing.
