how to validate input strings with regex patterns in Directus 9

Viewed 206

I'm using Directus CMS. This is one of the best CMS I've come to know so far in terms of usability, features, and extensibility and moreover, it is open source and free. The admin dashboard is amazing and responsive.

There is one thing I am struggling with for a while and that is validating strings against regex patterns such as validating a URL input, emails, pin codes or mobile number input.

If anyone has created a custom interface for the directus 9, please help me here!!

1 Answers

To achieve a specific field validation in Directus 9, please use regex validation when you create the fields in your collection's data model.

To test an email, for example :

  • Create a new field (called email)
  • Go to validation tab, select regex validation and enter this expression :
/^[a-zA-Z0-9.!#$%&’*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$/

this is a standard regex email validation, you can find other regex covering different usecases on https://regexpattern.com for example.

See the attached captures below :

Create a validation for field

API POST test result

Related