SEARCH

Extend field validation options with regular expressions
ionut
06.02.2018

Sometimes while building Drupal websites you might need to have some specific validation options for fields. For example if you want to make sure that a user will insert only a certain number of characters in a textfield or if you want to accept email addresses only from a certain domain or top level domain then regular expresion validation might be the best option.

A regular expresion is like a search pattern that can be used to perform searches on a string. A lot of programming languages incorporate libraries and functions that use special algorithms designed to work with these expressions. PHP is one of those languages and we can leverage this power in Drupal as well. How? Using the Drupal RegEx Field Validation module can be one way.

RegEx Field Validation is a Drupal module (supporting both Drupal 7 and Drupal 8) which provides an easy manner to add validation options to content type fields. After it is installed, the module provides a set of options on the configuration page of each text field from a content type:

  • Validate Field with RegEx. Checkbox to activate/deactivate the validation using a regular expression. This option shows/hides the textarea for the regular expression and the textfield for the custom error message.
  • Regular Expression. Textarea where the regular expression that is going to be used to validate the value of the field can be inserted.
  • Error message. Textfield that holds the custom error message that will be displayed when the validation is not passed.

Although there are a few other modules out there that can be used to validate the values from fields, this one is the simplest and it handles a specific problem. It has both Drupal 7 and Drupal 8 versions and it is actively maintained.

Here are a few examples of regular expressions that can be used:

\^[^<\x09]{0,100}\Z\

Text containing between 0 and 100 characters.

\^[AaBb]\Z\

1 character either A, a, B or b.

\^(http|https):\/\/.{2,80}\Z\

URL starting with “http” or “https” and contains between 2 and 80 characters.

\^.{2,40}\@.{2,50}\..{2,5}\Z\

Email address containing between 2 and 40 characters before the “@”, then between 2 and 50 characters as the domain name and between 2 and 5 as the top level domain

\^(ABC|DEF|GHI|JKL|MNO|PQR|STU|VWX)?\Z\

Accept a tree letter string that can be found in that list

\^([0-9]+(\.[0-9]{2})?)?\Z<\

Numeric with "." as decimal separator (29.99)

\^[0-9.]{1,8}\Z<\

Numerical value between 1 and 8 digits.

\^[^<\x09\x0a\x0d]{0,10}\Z\

Single line between 0 and 10 characters that should not contain HTML markup

\^[^<]{0,100}\Z\

Multiple lines between 0 and 100 characters that should not contain HTML markup

\^[^<\x09\x0a\x0d]{0,1000}\Z\

Text containing between 0 and 1000 letters, numbers and spaces

 

You can give it a try from here

This is relevant for …

drupal 8, drupal 7, field, validation, regular expressions, error

Post a comment