Skip to content
LogoLogo

How to validate a field

This guide shows you how to check what a visitor enters. Write the rule in JSON Logic. The $input variable holds the current value of the field.

Steps

Add a validation rule to createField():

import { createField } from '@picked/client/plugins/create-experience'
 
const quantityField = createField({
  id: 'quantity',
  name: 'quantity',
  type: 'number',
  label: 'Quantity',
  multiple: false,
  required: true,
  validation: {
    catch: {
      or: [
        {
          and: [
            { '>': [{ var: '$input' }, 0] },
            { '<=': [{ var: '$input' }, 999] },
          ],
        },
        { throw: 'Please enter a value between 1 and 999' },
      ],
    },
  },
})

The SDK runs the rule on blur and on submit.