JSP中email格式的判断

王朝java/jsp·作者佚名  2006-12-16
窄屏简体版  字體: |||超大  

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.");

}

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航