Java 使用正则表达式进行电子邮件验证
我发现我可以使用正则表达式验证电子邮件输入。然而,我不知道该把这些表达放在哪里。我是否将它们放在我的java控制器方法、实体类或JSP中?如果您的电子邮件是字符串,您可以编写如下内容来验证电子邮件:Java 使用正则表达式进行电子邮件验证,java,netbeans,Java,Netbeans,我发现我可以使用正则表达式验证电子邮件输入。然而,我不知道该把这些表达放在哪里。我是否将它们放在我的java控制器方法、实体类或JSP中?如果您的电子邮件是字符串,您可以编写如下内容来验证电子邮件: String email; . . // load the email string . if (email.matches("^[A-Za-z0-9_.]+[@][A-Za-z.]+$")) { . . . } else throw new EmailNotValidException
String email;
.
. // load the email string
.
if (email.matches("^[A-Za-z0-9_.]+[@][A-Za-z.]+$"))
{
.
.
.
}
else
throw new EmailNotValidException();
我认为您可以创建一些
EmailValidatorClass
类,在需要验证电子邮件地址时,您可以在项目中使用这些类:
import javax.mail.internet.InternetAddress;
import javax.mail.internet.AddressException;
import java.util.StringTokenizer;
/**
* A class to provide stronger validation of email addresses.
*
*/
public class EmailAddressValidator
{
public static boolean isValidEmailAddress(String emailAddress)
{
// a null string is invalid
if ( emailAddress == null )
return false;
// a string without a "@" is an invalid email address
if ( emailAddress.indexOf("@") < 0 )
return false;
// a string without a "." is an invalid email address
if ( emailAddress.indexOf(".") < 0 )
return false;
if ( lastEmailFieldTwoCharsOrMore(emailAddress) == false )
return false;
try
{
InternetAddress internetAddress = new InternetAddress(emailAddress);
return true;
}
vcatch (AddressException ae)
{
// log exception
return false;
}
}
/**
* Returns true if the last email field (i.e., the country code, or something
* like .com, .biz, .cc, etc.) is two chars or more in length, which it really
* must be to be legal.
*/
private static boolean lastEmailFieldTwoCharsOrMore(String emailAddress)
{
if (emailAddress == null) return false;
StringTokenizer st = new StringTokenizer(emailAddress,".");
String lastToken = null;
while ( st.hasMoreTokens() )
{
lastToken = st.nextToken();
}
if ( lastToken.length() >= 2 )
{
return true;
}
else
{
return false;
}
}
}
导入javax.mail.internet.InternetAddress;
导入javax.mail.internet.AddressException;
导入java.util.StringTokenizer;
/**
*提供更强的电子邮件地址验证的类。
*
*/
公共类EmailAddressValidator
{
公共静态布尔值isValidEmailAddress(字符串emailAddress)
{
//空字符串无效
如果(emailAddress==null)
返回false;
//没有“@”的字符串是无效的电子邮件地址
if(emailAddress.indexOf(“@”)<0)
返回false;
//没有“.”的字符串是无效的电子邮件地址
if(emailAddress.indexOf(“.”)小于0)
返回false;
if(lastEmailFieldTwoCharsOrMore(emailAddress)==false)
返回false;
尝试
{
InternetAddress InternetAddress=新的InternetAddress(电子邮件地址);
返回true;
}
vcatch(地址异常ae)
{
//日志异常
返回false;
}
}
/**
*如果最后一个电子邮件字段(即国家代码或其他内容)为
*例如.com、.biz、.cc等)的长度为两个字符或更多,这实际上是
*必须是合法的。
*/
私有静态布尔lastEmailFieldTwoCharsOrMore(字符串emailAddress)
{
如果(emailAddress==null)返回false;
StringTokenizer st=新的StringTokenizer(电子邮件地址“.”);
字符串lastToken=null;
而(st.hasMoreTokens())
{
lastToken=st.nextToken();
}
如果(lastToken.length()>=2)
{
返回true;
}
其他的
{
返回false;
}
}
}
仅使用正则表达式无法创建正确的电子邮件验证
下面介绍如何(几乎)正确验证电子邮件地址
电子邮件根据和进行验证。以下是电子邮件语法的简化形式定义:
addr-spec = local-part "@" domain
local-part = dot-atom-text / quoted-string
dot-atom-text = atom *("." atom)
atom = 1*atext
atext = ALPHA / DIGIT / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "/" / "=" / "?" / "^" / "_" / "`" / "{" / "|" / "}" / "~"
ALPHA = ASCII characters in the range ‘a’ (ACSII code 0x61) through ‘z’ (ACSII code 0x7A) and ‘A’ (ACSII code 0x41) through ‘Z’ (ACSII code 0x5A)
DIGIT = ASCII characters in the range ‘0’ (ACSII code 0x30) through ‘9’ (ACSII code 0x39)
specials = "(" / ")" / "<" / ">" / "[" / "]" / ":" / ";" / "@" / "\" / "," / "."
quoted-string = “ *(qcontent) “
qcontent = qtext / quoted-pair
qtext = 0x21 / 0x23-0x5B / 0x5D-0x7E; All ASCII printable characters instead of “ and \
quoted-pair = \ (VCHAR / WSP)
VCHAR = 0x21-0x7E; All ASCII printable characters
WSP = 0x20 / 0x09; ASCII Space or tab characters
另外,将域限制为255个ASCII符号,将标签限制为63个ASCII符号
Apache commons验证库(在问题的评论中提到)是一个良好的开端。它包含域验证和电子邮件验证,尽管上次我检查时,它在电子邮件验证中有一些错误。为什么不使用commons validator?请参考以下问题,给出一些比例:@Artefactor commons validator?这是进口货还是进口货?我是java新手>@RUi,不过你得使用一个额外的库。
addr-spec = local-part "@" domain
local-part = dot-atom-text / quoted-string
dot-atom-text = atom *("." atom)
atom = 1*atext
atext = ALPHA / DIGIT / "!" / "#" / "$" / "%" / "&" / "'" / "*" / "+" / "-" / "/" / "=" / "?" / "^" / "_" / "`" / "{" / "|" / "}" / "~"
ALPHA = ASCII characters in the range ‘a’ (ACSII code 0x61) through ‘z’ (ACSII code 0x7A) and ‘A’ (ACSII code 0x41) through ‘Z’ (ACSII code 0x5A)
DIGIT = ASCII characters in the range ‘0’ (ACSII code 0x30) through ‘9’ (ACSII code 0x39)
specials = "(" / ")" / "<" / ">" / "[" / "]" / ":" / ";" / "@" / "\" / "," / "."
quoted-string = “ *(qcontent) “
qcontent = qtext / quoted-pair
qtext = 0x21 / 0x23-0x5B / 0x5D-0x7E; All ASCII printable characters instead of “ and \
quoted-pair = \ (VCHAR / WSP)
VCHAR = 0x21-0x7E; All ASCII printable characters
WSP = 0x20 / 0x09; ASCII Space or tab characters
domain = subdomain / " "
subdomain = label / subdomain "." label
label = let-dig *(*(ldh-str) let-dig)
ldh-str = let-dig-hyp / let-dig-hyp ldh-str
let-dig-hyp = let-dig / "-"
let-dig = ALPHA / DIGIT