Knowledge Centre

Extend field validation with regular expressions

by editor | 06.02.2018

Extend field validation with regular expressions

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 text field or if you want to accept email addresses only from a certain domain or top-level domain then regular expression validation might be the best option.

A regular expression 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 text area for the regular expression and the text field 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:

Extend field validation with regular expressions


Text containing between 0 and 100 characters.



1 character either A, a, B or b.



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



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



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



Numeric with "." as decimal separator (29.99)



A numerical value between 1 and 8 digits.



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



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



Text containing between 0 and 1000 letters, numbers and spaces


You can give it a try from here

  • Knowlegde
    Knowledge Centre
    Replace a field's content based on another field using AJAX
  • Knowlegde
    Knowledge Centre
    Port your sites to Drupal 9
  • Knowlegde
    Knowledge Centre
    Drupal 8 or Drupal 9

Post a Comment.