开发者

Trouble using a regex to validate email addresses with double '@'

In order to validate email addresses I am using the following regex from www.regular-expressions.info:

[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?

The problem using the above regex is that I can succefully validate also email addresses with double '@' like these:

name@surname@gmail.com # Note the double 开发者_如何学Go'@'
test@gmail@com.com

... that I don't want. So, how can I adjust that?


UPDATE

I discover also that regex doesn't refuse email addresses like these:

name@gmail
test@surname@gmail


Your current regex matches any string that has a valid email in it. Your input string nome@surname@gmail.com has a valid email surname@gmail.com.

You need to add start anchor(^) and end anchor($) to the regex so that the regex matches the complete string and not a proper substring of it.

Try:

^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$


assuming you "might" be using .net, here's the regex I use for validating email addresses.

^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})$
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜