back

Knowlegde

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
Top
default
\^[^<\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<\

 

A numerical value between 1 and 8 digits.

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

 

A 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

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

Post a Comment.