Hi everyone,
I was trying to validate an email field properly in AppSheet, and while I searched the community, I couldnโt find something that covered all the key cases.
So I came up with this expression that works without using REGEX, and it checks for the essentials (and a bit more):
That the field is not blank
That it contains an @
That thereโs at least one .
after the @
That there are at least 2 characters after the last dot, making sure the domain extension is valid (like .com
, .org
, .net
, etc.), and filtering out suspicious cases like someone@mail.
or user@web.c
AND(
ISNOTBLANK([email]),
CONTAINS([email], "@"),
CONTAINS(RIGHT([email], LEN([email]) - FIND("@", [email])), "."),
LEN(
INDEX(
SPLIT(
RIGHT([email], LEN([email]) - FIND("@", [email])),
"."
),
COUNT(
SPLIT(
RIGHT([email], LEN([email]) - FIND("@", [email])),
"."
)
)
)
) >= 2
)
Because according to ICANN (the global domain authority), there are no valid top-level domains (TLDs) with a single character. So this rule helps prevent typos or invalid emails that could slip through.
Suggestion of @Suvrutt_Gurjar
I'm also leaving a formula up here in case the comment ever gets pushed down, by the authority Suvrutt_Gurjar, much more efficient than the one I presented and without so many internal evaluations.
AND(
LEN(SUBSTITUTE(@&EXTRACTDOMAINS([Email]), EXTRACTMENTIONS([Email]),""))>=3,
ANY(EXTRACTEMAILS([Email]))=[Email]
)
Here is an even more compact suggestion that allows us to achieve the same result, also suggested by our colleague @Suvrutt_Gurjar
Here are his words
"Please note that for checking if a field is left blank, a default approach in AppSheet is to make the field "required." So my suggested expression does not include checking it for a blank field."
AND(
LEN([email]) - FIND(".", [email])>=2,
ANY(EXTRACTEMAILS([email]))=[email]
)
Hi @Gustavo_Eduardo ,
Have you explored the EXTRACT() functions?
With EXTRACT() functions you could do the needful with a much shorter expression, something like
AND(
LEN(SUBSTITUTE(@&EXTRACTDOMAINS([Email]), EXTRACTMENTIONS([Email]),""))>=3,
ANY(EXTRACTEMAILS([Email]))=[Email]
)
Additionally valid_if using EXTRACTEMAILS() also checks that there are no other special characters such as say $, %, & etc. are entered in email that are not allowed.
You have no idea how much I appreciate this, my friend @Suvrutt_Gurjar ! I changed the formula, and yours is more efficient. Thank you so much for sharing it with the community! A hug!
You are welcome @Gustavo_Eduardo .
Please note that for checking if a field is left blank, a default approach in AppSheet is to make the field "required." So my suggested expression does not include checking it for a blank field.
Also here is another version of the expression that I believe is still more compact.
AND(
LEN([Email])- FIND(".", [Email])>=2,
ANY(EXTRACTEMAILS([Email]))=[Email]
)
Thank you very much my friend!!