Can I validate email addresses entered on a field in an eDocBuilder template?

Please note that this example shows one possible approach for using regular expressions to validate email addresses. The validation of email addresses using regular expressions can be a complex and sometimes controversial subject. This article shows one possible solution to validating email addresses with regular expressions, that works well enough in many situations encountered by our clients, but is not meant to be a final or absolute answer to validating emails with regular expressions.
 
The example template that you can try here checks to see if the email address entered on the template matches a valid email address pattern specified by a regular expression, and if the input email matches to one of three possible domain addresses. Using a malformed email address, or using a domain address that is not included in the script will produce different errors that will be shown on the template when previewed.

The regular expression pattern

This example uses an eDocBuilder Forms template with a field set for Field Scripting or Static Text, using VB Script with a replaceRegex() function used in a couple of different ways in the script. This example will not work with Interactive Designer templates, and will only work with Forms templates.
 
The regular expression being used to validate the email address input in this situation is shown below:
 
^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$
The replaceRegex() function in eDocBuilder VB Script looks at the input provided in a named field on the template, checks if the regular expression matches to the input, and gives some return value. The replaceRegex() function only returns a value that is specified in the third option in that function. It cannot return, for example, a boolean TRUE or FALSE value if the pattern matches, but leaving the return value in the third option empty is almost the same thing.

Checking if the input is an email address

The script below returns nothing if the pattern is matched, so embedding the function in an ifs() statement gives us an empty return if the email pattern is matched. The ifs() statement checks then if the replaceRegex() function returns an empty value -- the TRUE condition -- and if so, it returns the email address input. If the replaceRegex() function does not match the input email address, it returns the input text as it is -- the FALSE condition.
ifs(replaceRegex(inputField,"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$","")="",inputField,"Not a valid email.")
If the email address input is good, the script statement returns the input email address as it is. If the email address is bad, the script statement returns an error string as a FALSE condition. This gets us the first part of the solution, validating if the input email address matches the regular expression, and showing an error if it does not match.

Checking if the input matches the valid domains

The next part of the problem is to also validate if the input email address matches to one of three possible domain names that should be allowed for email address input on this template. The domains that are being matched to in this example are: aleyant.com, pressero.com and edocbuilder.com.
 
To achieve that, we use the portion of the regular expression that matches to the first part of the email before @, followed by the domain name, using that three times as part of a single ifs() statement. The three possible conditions comprise the first part of the ifs() statement. Three parts joined by the OR operator in the first option of the ifs() statement.
 
ifs(replaceRegex(inputField,"^[a-zA-Z0-9_.+-]+@aleyant.com","")="" OR replaceRegex(inputField,"^[a-zA-Z0-9_.+-]+@pressero.com","")="" OR replaceRegex(inputField,"^[a-zA-Z0-9_.+-]+@edocbuilder.com","")="",inputField,"Not a valid email domain.")
If the email address input matches to one of the three domain names, then the email address input is returned from the statement as entered. If the email address input does not match one of the three domain names, the error message is returned from the statement.

Creating the final composite statement

You can see in the statement above that you can embed statements within other statements in eDocBuilder VB Script. Three replaceRegex() statements are nested within the ifs() statement as it's conditions. This can get complex to follow and to write, so writing out each step separately before combining the statements is a good idea.
 
To get the final VB Script statement used on the example template linked above, the second statement was nested in the TRUE result of the first statement. Instead of the first statement just returning the email address that was input, it now drills down and checks the input using the second statement. The resulting statement is somewhat difficult to read, but it works when used on the example template.
 
ifs(replaceRegex(inputField,"^[a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+$","")="",ifs(replaceRegex(inputField,"^[a-zA-Z0-9_.+-]+@aleyant.com","")="" OR replaceRegex(inputField,"^[a-zA-Z0-9_.+-]+@pressero.com","")="" OR replaceRegex(inputField,"^[a-zA-Z0-9_.+-]+@edocbuilder.com","")="",inputField,"Not a valid email domain."),"Not a valid email.")
 
If you are going to use the statement above as it is written, you would need to replace inputField with the name of the input field for the email address from your own template. Then change the three domain names to match what domain names will be valid input on your template. It is of course possible to edit the domain name checks to account for one, or two, or four, or more domain names that you might need to be validating.

Support and Professional Services

Please note that this is advanced example of using scripting in an eDocBuilder template to achieve a specific result. While our support staff can answer questions relating to template scripting, they cannot do custom template scripting work for our clients on demand. If you have any questions relating to scripting in eDocBuilder templates, or want to confirm if something is possible to do, please open a support ticket at support.aleyant.com. If you want us to create a template for you, using  advanced scripting, please open a ticket at support.aleyant.com and mark it as Professional Services, so someone can review and quote your project.

Related articles