How to use regular expressions for custom field validation
Many form fields that you can add to a Blitzen form such as email fields, number fields, and website fields, have included validation patterns to prevent users from entering invalid data. For an email field, this would ensure that there is an '@' symbol, and that the general pattern of an email address is correctly followed.
In some use-cases, you may want to create your own validation pattern for a form field. With support for regular expressions (regex), Blitzen lets you do this.
Let's assume that you wanted to have a form field that only allowed lowercase letters, and nothing else (not even spaces). In this scenario, your regular expression would look like this:
If you're new to regex, this probably looks fairly confusing. However, if we break it down character by character, it should become somewhat clear how it works.
- the square braces that encapsulate a-z are used to specify the character set. In this case, our character set is any lower case letter
- inside the square braces, a-z is used to specify that we want to match any lowercase letter. Note that the hyphen in a-z is a convenience character so that we don't have to type out [abcdefghijklmnopqrstuvwxyz].
- Lastly, the plus symbol or + means that we want to allow the a-z pattern to be matched one or more times.
Now let's say you wanted to allow uppercase characters as well as lowercase. We would change our regex to look like this:
Note that all we did was add in A-Z.
If we wanted to add in numbers as well, we would change the regex as follows:
Finally let's assume we also want to allow spaces:
Note that the only change we made from the previous regex was to add a space after the 9.
Once you have determined which regex pattern fits your use-case, you can add it to a single line text field by clicking Add Regular Expression under the Validation & Autofill menu.
We also recommend that you enter a custom error message for when validation fails. Ideally, a good custom error message will allow the user who is filling out your form or survey know exactly what to do in order to enter a valid response. Directly under the input field where you would enter your regex pattern, you will see another input where you can enter your custom error message.
Tying it all together, if we go back to our very first regex pattern, here's an example of what it could look like inside of Blitzen:
The preceding examples were very basic, and typically the need for regex will be more complicated. Often you will be able to find examples online to fit your use case, but if you are struggling feel free to contact us at firstname.lastname@example.org.