JSP中email格式的判断

rain_ok 2002-05-15 09:45:51
加精
请问怎样才可以判断email的格式,就是判断有@符号,我用过
if(mail.indexOf('@')<=0)
out.println("email address wrong!");
但是不可以,还有就是咋可以让它限制用户注册时在email栏输入中文,小妹谢谢各位了
...全文
99 点赞 收藏 5
写回复
5 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
donny2000 2002-05-16
用正则表达式
回复
HuangBin 2002-05-16
我给你个简单的:
public boolean isEmail(String email){
int pos;
pos=email.indexOf('@');
if (pos==-1)
return false;
else{
pos=email.indexOf('.');
if (pos==-1)
return false;
else
return true;
}
}
回复
Andrawu 2002-05-16
此帖里有你想要的判断email的格式的方法。
http://www.csdn.net/expert/topic/654/654432.xml?temp=.3112146
回复
Broadsea 2002-05-16
找到了!我自己的EMIAL校验就是参照SUN下面的例子做的。

Email Validation

The following code is a sample of some characters you can check are in an email address, or should not be in an email address. It is not a complete email validation program that checks for all possible email scenarios, but can be added to as needed.


/*
* Checks for invalid characters
* in email addresses
*/
public class EmailValidation {
public static void main(String[] args)
throws Exception {

String input = "@sun.com";
//Checks for email addresses starting with
//inappropriate symbols like dots or @ signs.
Pattern p = Pattern.compile("^\\.|^\\@");
Matcher m = p.matcher(input);
if (m.find())
System.err.println("Email addresses don't start" +
" with dots or @ signs.");
//Checks for email addresses that start with
//www. and prints a message if it does.
p = Pattern.compile("^www\\.");
m = p.matcher(input);
if (m.find()) {
System.out.println("Email addresses don't start" +
" with \"www.\", only web pages do.");
}
p = Pattern.compile("[^A-Za-z0-9\\.\\@_\\-~#]+");
m = p.matcher(input);
StringBuffer sb = new StringBuffer();
boolean result = m.find();
boolean deletedIllegalChars = false;

while(result) {
deletedIllegalChars = true;
m.appendReplacement(sb, "");
result = m.find();
}

// Add the last segment of input to the new String
m.appendTail(sb);

input = sb.toString();

if (deletedIllegalChars) {
System.out.println("It contained incorrect characters" +
" , such as spaces or commas.");
}
}
}
回复
Broadsea 2002-05-16
没这么简单,java.sun.com上有标准的例程,如果需要的话,可以帮你找找。
回复
发动态
发帖子
Web 开发
创建于2007-09-28

7.9w+

社区成员

Java Web 开发
申请成为版主
社区公告
暂无公告