I having the following code:
if not (email:match("[A-Za-z0-9%.]+@[%a%d]+%.[%a%d]+")) then
print(false)
end
It doesn't currently catch
"test@yahoo,ca" or "[email protected],com"
as an error.
I thought by limiting the input to %a
- characters and %d
- digits, I would by default catch any punctuation, including commas.
But I guess I'm wrong. Or there's something else that I'm just not seeing. A second pair of eyes would be appreciated.
In the example of "[email protected],com"
, the pattern matches [email protected]
and stops because of the following ,
. It's not lying, it does match, just not what you expected. To fix, use anchors:
^[A-Za-z0-9%.]+@[%a%d]+%.[%a%d]+$
You can further simplify it to:
^[%w.]+@%w+%.%w+$
in which %w
matches an alphanumeric character.
I had a hard time finding a true email validation function for Lua.
I couldn't find any that would allow some of the special cases that emails to allow. Things like +
or quotes are actually acceptable in emails.
I wrote my own Lua function that could pass all the tests that are outlined in the spec for email addresses.
http://ohdoylerules.com/snippets/validate-email-with-lua
I also added a bunch of commentd, so if there is some strange validation that you want to ignore, just remove the if statement for that particular check.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With