Email != Empty
Find @ and contain only 1
Split Local & Domain Part
Check Illegal characters and Local < 64
Checking an email for validity regarding the specifications for valid addresses is a nightmare. In almost all cases where people try to implement this they are too strict and prevent users with VALID emails address to register.
Simply let the user enter the address twice and check that it contains an ‘@’ so a user at least tried to enter an email.
- check @ character
- check if only one at-sign is present
- Split string based on @ char i.e local part and domain part of address
- Retrieve LocalPart and DomainPart from List
- Check if the DomainPart contains dot
- Split DomainPart list based on dot to check if dot is not the first or last character
- Check the value of DomainPart after dot, should have length either 2 or 3
There are many ways to perform e-mail validation. The exact rules for validation are complex. You have to read a couple of RFC documents and try to implement them.
The first question you should ask: do I need to re-invent the wheel or has this already been implemented before.
Another question is how to implement the validation. Is a Microflow the best tool ? Microflows are supposed to be easy to understand. You can discuss the logic of a Microflow with a business user. Should we discuss e-mail validation with a business user ? I do not think so.
You may find different e-mail validators written in Java. The native package javax.mail.internet contains a class InternetAddress with a method validate(). It implements most of the rules defined by the RFC’s. But it is not perfect.
It is easy to create a Java action that implements this method. Just read the docs, set-up Eclipse and add a few line to the auto-generated code for the action:
The validate () method throws an error in case the supplied string is not a valid e-mail.
In my demo application I have an entity Customer with a property Email.
I have used the new “on change” event that is triggered on each character input.
The Microflow is very simpel. If the field is empty we consider this as “no-error”. When the field contains text we call the Java action. I have added a custom error handler that shows a message to the user.
When running the application this results in
Above solution is easy to implement, easy to understand and works better than many do-it-yourself validations.
However it is not good enough. Above validations are only looking at the syntax. But there is more to consider: does the mailbox exist ? Is it a dispostable e-mail address ? Is the mailbox full ?
If you want a high quality customer database with valid e-mail addresses you have to go at least one step further.
Many on-line companies offer e-mail validation services. Typically you call a REST API, supply the e-mail address and they will perform a bunch of quality tests. The return value of the REST call give you the answer.
I picked (at random) one of the service providers that have a “free” API (free during test – then you have to pay). So I used MailerCheck. (I’m not saying they are good are bad – it is just for showing the concept). The API is documented at https://www.mailercheck.com/api/email-endpoints#single-email-verification.
You need to create an account and a free API key.
Calling the API and waiting for the reply takes a couple of seconds (they try make a SMTP connection to the target server). So you cannot call this validation at every keystroke. I called it when leaving the e-mail field.
The Microflow calls the REST API.
The response is a simple JSON structure with only one property
I created an import mapping to handle the response:
The REST call uses this import mapping
In the flow we test on the property status. The list of possible values are
The REST call takes a few seconds to execute. The user must be aware of this.
I decided to show a progress message. This is configured in the settings of the microflow call
As a test I inserted a typo in my real mail address (email@example.com) → firstname.lastname@example.org.
For the Java validator this is a valid e-mail address.
When leaving the field:
And after a few seconds
Is this good enough ? Not yet.
After this validation you should send an e-mail to the validated address with some unique url in the body. The recipient should click on the link to prove that he has received the mail. I have not implemented this extra step.
So, this is perhaps not the answer expected by the challenge. But I’m convinced it is a good approach in e-mail validation.
- Count the number of '@' characters - it should be one.
- Check the Domain and Local address lengths - the local is limited to 64 characters.
- Check that the address ends with a non '.' character, but the Domain must contain a minimum of one '.'
- Check for illegal characters
Since Mendix is aiming to be a Low Code platform, I would go to the Sprinter / Forurm / Ideas and post an Idea to get an Appstore component that can provide this functionality.
Any Mendix app requiring users from outside the organization you build the app for need this functionality.
This answer probably does not make me Maker of the Month, only hoping that the Idea is going to be picked up, In some months from now, I will have the shortest answer :-)
This answer is intended to make everyone think different (I do like the maker of the month challenge and it is great to see all answers being posted), very often I see people jump into coding on low code, sharing a problem ahead in the Forum Idea can help to make the Mendix product better. If an Idea is implemented, we can refactor complex code to just calling a function (microflow/nanoflow/java) from an appstore module to do this.
As per my below screenshot, I have an entity called Registration with Email as a attribute.
I really prefer validating email addresses via activation mail. But for rudimentary checking I would propose this solution as it guarantees that all wrong parts are found before showing the validation message:
I have an Account object, which contains Email attribute. During save process of registration, I have called the following sub microflow activity for Email address validation.
My check has a few steps:
if it fits all checks above the result is a boolean true else a boolean false.
Hey Team Mendix!
I did the following checks:
If the input fails the one of the checks, it will return a validation message about what's wrong or missing.
Check minimum valid length of an Email.
If whether email has @ character.
Check whether Dot is present, and that too minimum 1 character after @.
Check whether Dot is not the last character and dots are not repeated.