Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lua pattern matching for email address

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.

like image 976
dot Avatar asked Feb 10 '23 05:02

dot


2 Answers

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.

like image 182
Yu Hao Avatar answered Feb 19 '23 07:02

Yu Hao


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.

like image 33
james2doyle Avatar answered Feb 19 '23 05:02

james2doyle