Skip to main content

Custom Validators

Create custom field validation rules using REGEX to ensure form submissions meet your requirements for data quality and format.

Custom validators allow you to create specific requirements for fields to be considered valid. Use regular expressions to validate the format of user input, or use a comparison validator to require one field to match another.

Creating custom field validation rules with regular expressions

In Formulayt, custom validation rules can be added to fields in the manage fields area. This allows you to create very specific requirements for fields to be considered valid.

Note: Custom validators can only be added to visible fields that users interact with. They cannot be added to hidden fields.

These validation rules use regular expressions to ensure that the input entered by a user matches the required format. You should be comfortable writing regular expressions before using this feature. If you're new to regular expressions, you can check out one of the many tutorials online. Additionally, Regex101 provides an excellent tool for quickly building and testing regular expressions before using them on your Formulayt account.

If a custom validator has been added, any value that the user enters into the field must match against the regular expression to be considered valid.

Regular expression validators should always be tested in preview mode before publishing them to ensure they work as intended and don't inadvertently prevent all form submissions.

In addition, care should be taken to specify a meaningful error message that explains to the user exactly what they need to do to pass validation.

For example:

"Please enter a business email address"

Is much better than:

"Email address not valid"

Example regular expressions

The examples below show some useful regular expressions that you can adapt for your needs:
​

Allow only alpha characters

To allow only letters to be entered, you can use the following:

^([a-zA-Z]*)$

To allow spaces as well, you can amend this to:

^([a-zA-Z\s]*)$

Allow only numbers

To allow only numbers, you can use the following:

^([0-9]*)$

Block specific email domains

This is achieved using a negative lookahead:

.+@(?!example1\.|example2\.|example3\.)

The domains are added in this section: example1. example2. and example3. Importantly, these must be pipe separated, and each period must be preceded by a backslash. This will also match any TLD variant, for example, example1.co.uk or example1.com would both be blocked by this regular expression.

Here is the same regular expression with common personal email domains which you can adapt as necessary for your purposes:

.+@(?!gmail\.|hotmail\.|me\.|ymail\.|yahoo\.|mac\.|live\.|msn\.|outlook\.)

Block URLs

This is also achieved using a negative lookahead to block anything that matches a URL pattern:

^(?!.*(telnet|ftp|https?):\/\/(?:[a-z0-9][a-z0-9-]{0,61}[a-z0-9]\.|[a-z0-9]\.)+[a-z]{2,63})

Comparison validator

The Comparison validator allows you to require two fields, on the same form, to match exactly. This is useful if you need end users to confirm a value they have already entered, such as an email address.

For example, if you have:

  • Email (email)

  • Confirm email (email_confirm)

To setup the comparison validator:

  • Go to Setup > Fields

  • Select the field that you want to match exactly to another field

    • E.g. Confirm email (email_confirm)

  • Go to the Validation tab and click Add a comparison validator.

  • Enter a Validator name/key

    • This will be used to identify the error message in Translations

  • Select the field you want to match against

    • E.g. Email (email)

  • Enter the error message that will appear to users should the values not match

    • E.g. “Email addresses do not match. Please check and try again.”

  • Update and Publish

Important considerations

  • The field being compared against must be on the same form.

  • The comparison includes case and spaces, but skips empty values. Make the confirmation field required to prevent blank submissions.

Did this answer your question?