When adding an email field to a form, you may want to restrict the addresses that can be added to a domain or set of domains. To do this we use the Regular Expression Pattern property of either the email or text field.
Where to go
The first step is to go to your field properties by clicking the cog icon for the relevant field in the form builder. Then go to the Validation tab and find the Regular Expression Pattern property.
Build up your Regular Expression Pattern (RegEx)
The next step is to build up your expression. You can find many RegEx pattern reference guides online if you want to learn this for yourself (e.g. this one from w3schools). However, for the purposes of this article we will be walking you through the exact steps, so you don't need to know everything upfront.
For our example let's say that only emails ending in "@example.com" are allowed to be entered in this field.
First of all we need to specify that we don't care what appears at the start of the email. In our use-case we only care that the email comes from example.com, we don't care if the address starts with "alice", "bob" or "charlie". To do this, we use the metacharacter for 'any character':
.
and add the quantifier for any number of occurrences:
*
to get:
.*
Now we add our domain:
.*@example.com
And then add a backslash before any special characters so they are matched literally:
.*\@example\.com
Now we just need to make sure that this appears at the end of the input field by adding the 'end of string' quantifier:
$
To get:
.*\@example\.com$
Optionally, we can also make sure certain characters are reviewed as case insensitive, by using a strict find for both upper and lower case of the characters:
[Aa]
For the relevant character (or characters):
.*\@[Ee]xample\.com$
If we use this as our Regular Expression Pattern property, and save both the field and form, when the user next opens the form they will see something like this when they start to write their email in:
This will go away once the pattern has been matched:
Matching more than one domain
To match more than one domain we can chain our test strings with the 'or' operator
|
To get the following:
.*\@example\.com$|.*\@example\.co\.uk$|.*\@example\.com\.au$
Or alternatively, we can only chain the aspects of the strings that differ:
.*\@example(\.com|\.co\.uk|\.com\.au)$
Using a custom error message
Our standard error message for when the RegEx is not matched is just a simple 'does not match the pattern'. This may not be of great help to a user, so we recommend adding a Custom Error Message to describe to the user what type of email they should be using. This is found just below the Regular Expression Pattern Property on the same field.
For example if we use the following for the Custom Error Message:
Must be an Example Firm email address
The user is more effectively informed:
Comments
0 comments
Article is closed for comments.