4

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 "test@test1.test2,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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
dot
  • 14,928
  • 41
  • 110
  • 218
  • Please check this post: http://stackoverflow.com/questions/21040325/email-address-validation-using-corona-sdk. Does it resolve your problem? – Wiktor Stribiżew May 06 '15 at 17:48
  • That "email address" fails to match that pattern for me. That being said email validation is **not** simple if you want to actually allow all valid email addresses and this pattern **will** fail to match any number of valid addresses. – Etan Reisner May 06 '15 at 17:49
  • @stribizhev, no it doesn't. actually, that was the post I initially used to create my regex, when I found that my app has this bug. Since then, I've been playing around with other patterns, and the latest one I came up with is the one in my question. – dot May 06 '15 at 17:50
  • The second test email address matches because you aren't anchoring the match to the start and end of the string so when the match runs out of valid characters at the `,` in the second test it doesn't think that is a problem. – Etan Reisner May 06 '15 at 17:51
  • 1
    Read the comment by `lhf` on that question to see just how hard this actually is to do correctly. Then give up. =) – Etan Reisner May 06 '15 at 17:51
  • @EtanReisner i see. ok well, i don't feel so bad then about scratching my head over this one. – dot May 06 '15 at 17:56
  • Hm? No. Your specific problems are not because email addresses are complicated. Your first "problem" doesn't seem to exist and I just explained your second problem in my previous comment. – Etan Reisner May 06 '15 at 17:57
  • 1
    https://eval.in/private/31847d49943ec6 – Etan Reisner May 06 '15 at 18:00
  • 2
    What about anchors ^ and $ ? https://eval.in/private/72d09e8e914b15 – Egor Skriptunoff May 06 '15 at 19:12
  • @EgorSkriptunoff, yes, that works.. I think that's what Etan was referring to as well, although he didn't include it in his example. Thanks! – dot May 06 '15 at 20:17
  • As already mentioned, this is a very hard problem to get it right 100%. You can keep playing with minor pattern modifications here and there but what seems to work for some email addresses will fail for others. Example for your current test: "test@yahoo..ca" So, without extensive analysis of each email address, you can only hope for a statistically correct result that may work well for a large number of emails but not all, either failing to accept, or failing to reject. – tonypdmtr May 06 '15 at 21:07

2 Answers2

5

In the example of "test@test1.test2,com", the pattern matches test@test1.test2 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.

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
3

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.

james2doyle
  • 1,399
  • 19
  • 21