我应该如何验证一个电子邮件地址?电子邮件地址

由网友(落影执迷离)分享简介:什么是一个很好的技术用于Android的确认电子邮件地址(例如,从用户输入字段)? org.apache.commons.validator.routines.EmailValidator似乎没有用。是否有任何其他库这样做,它们包含在Android的已经否则我将不得不使用正则表达式?解决方案 不要使用一个reg-EX...

什么是一个很好的技术用于Android的确认电子邮件地址(例如,从用户输入字段)? org.apache.commons.validator.routines.EmailValidator似乎没有用。是否有任何其他库这样做,它们包含在Android的已经否则我将不得不使用正则表达式?

解决方案

不要使用一个reg-EX。

显然,下面是一个reg-EX是正确验证符合 RFC 2822大多数电子邮件地址,(并仍在像user@gmail.com.nospam失败,也将org.apache.commons.validator.routines.EmailValidator)

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|[x01-x09x0bx0cx0e-x7f])+)])

可能是为了验证电子邮件只发送一封确认邮件提供的地址最简单的方法,它是反弹那么它是无效的。

如果您要执行一些基本的检查,你可以只检查它的形式 * @ *

YL Mail Verifier破解版下载 YL Mail Verifier 邮箱验证器 v4.0免费版 ucbug软件站

如果你有一些业务逻辑的具体验证,那么你可以执行,使用正则表达式,如:必须是一个gmail.com帐户或什么的。

What's a good technique for validating an e-mail address (e.g. from a user input field) in Android? org.apache.commons.validator.routines.EmailValidator doesn't seem to be available. Are there any other libraries doing this which are included in Android already or would I have to use RegExp?

解决方案

Don't use a reg-ex.

Apparently the following is a reg-ex that correctly validates most e-mails addresses that conform to RFC 2822, (and will still fail on things like "user@gmail.com.nospam", as will org.apache.commons.validator.routines.EmailValidator)

(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[x01-x08x0bx0cx0e-x1fx21x23-x5bx5d-x7f]|[x01-x09x0bx0cx0e-x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?).){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[x01-x08x0bx0cx0e-x1fx21-x5ax53-x7f]|[x01-x09x0bx0cx0e-x7f])+)])

Possibly the easiest way to validate an e-mail to just send a confirmation e-mail to the address provided and it it bounces then it's not valid.

If you want to perform some basic checks you could just check that it's in the form *@*

If you have some business logic specific validation then you could perform that using a regex, e.g. must be a gmail.com account or something.

阅读全文

相关推荐

最新文章