一个java编程 如何用java编写一个验证邮箱是否正确的程序 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 public final static void isEMail(final ActionMessages errors, final String formName, final String propertyName, final String value) { RE regexpValid = null; RE regexpInvalid = null; try { regexpValid = new RE("^\\S+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$"); regexpInvalid = new RE("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"); if ((value == null) || !regexpValid.isMatch(value) || regexpInvalid.isMatch(value)) { addError(errors, formName, propertyName, NO_EMAIL); } } catch (REException exception) { exception.printStackTrace(); } }这个是我做的项目中的mail邮箱地址是否合法的验证。希望对楼主有帮助 你是想验证邮箱格式的真确性,还是想验证邮箱的合法性和正确存在啊???我把2种的代码都贴出来,希望对你有帮助!验证格式的:public class RegisterCheck { /** * 验证输入的邮箱格式是否符合 * @param email * @return 是否合法 */public static boolean emailFormat(String email) { boolean tag = true; final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; final Pattern pattern = Pattern.compile(pattern1); final Matcher mat = pattern.matcher(email); if (!mat.find()) { tag = false; } return tag; }} 验证合法性和是否存在的:import java.io.*;import javax.mail.MessagingException;import javax.mail.internet.AddressException;import javax.mail.internet.InternetAddress;import javax.mail.internet.MimeUtility;/*** Utility methods once in TextUtils that require the mail.jar library.** @author $Author: hani $* @version $Revision: 1.1.1.1 $*/public class MailUtils { //~ Methods //////////////////////////////////////////////////////////////// /** * Decode binary data from String using base64. * * @see #encodeBytes(byte[]) */ public final static byte[] decodeBytes(String str) throws IOException { try { ByteArrayInputStream encodedStringStream = new ByteArrayInputStream(str.getBytes()); InputStream decoder = MimeUtility.decode(encodedStringStream, "base64"); ByteArrayOutputStream decodedByteStream = new ByteArrayOutputStream(); byte[] buffer = new byte[256]; while (true) { int read = decoder.read(buffer); if (read == -1) { break; } decodedByteStream.write(buffer, 0, read); } decodedByteStream.flush(); return decodedByteStream.toByteArray(); } catch (MessagingException me) { throw new IOException("Cannot decode data."); } } /** * Encode binary data into String using base64. * * @see #decodeBytes(java.lang.String) */ public final static String encodeBytes(byte[] data) throws IOException { try { ByteArrayOutputStream encodedByteStream = new ByteArrayOutputStream(); OutputStream encoder = MimeUtility.encode(encodedByteStream, "base64"); encoder.write(data); encoder.flush(); return new String(encodedByteStream.toByteArray()); } catch (MessagingException me) { throw new IOException("Cannot encode data."); } } /** * Verify that the given string is a valid email address. * "Validity" in this context only means that the address conforms * to the correct syntax (not if the address actually exists). * * @param email The email address to verify. * @return a boolean indicating whether the email address is correctly formatted. */ public final static boolean verifyEmail(String email) { if (email == null) { return false; } if (email.indexOf('@') < 1) { return false; } try { new InternetAddress(email); return true; } catch (AddressException e) { return false; } }} 谁知道哪里有java语言程序设计 基础篇 第五版的练习答案啊~谢谢! 关于jar包的设置 Java的学习路线 是否enum不被tomcat5支持? java的一个问题 java高手请进,请教个问题 关于包的问题,package怎么会包不了?? 请问如何将String转换成InputStream 学了一段时间了,想实践一下 又是一个很菜的问题但是不得不麻烦大哥大姐了啊~ 速度速度! java 调用 dll 问题 另一个有关classpath和package的问题
final String formName,
final String propertyName,
final String value) {
RE regexpValid = null;
RE regexpInvalid = null; try {
regexpValid = new RE("^\\S+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
regexpInvalid = new RE("(@.*@)|(\\.\\.)|(@\\.)|(\\.@)|(^\\.)"); if ((value == null) || !regexpValid.isMatch(value) ||
regexpInvalid.isMatch(value)) {
addError(errors, formName, propertyName, NO_EMAIL);
}
} catch (REException exception) {
exception.printStackTrace();
}
}这个是我做的项目中的mail邮箱地址是否合法的验证。希望对楼主有帮助
public class RegisterCheck { /** * 验证输入的邮箱格式是否符合 * @param email * @return 是否合法 */public static boolean emailFormat(String email) { boolean tag = true; final String pattern1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$"; final Pattern pattern = Pattern.compile(pattern1); final Matcher mat = pattern.matcher(email); if (!mat.find()) { tag = false; } return tag; }} 验证合法性和是否存在的:
import java.io.*;import javax.mail.MessagingException;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeUtility;
/**
* Utility methods once in TextUtils that require the mail.jar library.
*
* @author $Author: hani $
* @version $Revision: 1.1.1.1 $
*/
public class MailUtils {
//~ Methods //////////////////////////////////////////////////////////////// /**
* Decode binary data from String using base64.
*
* @see #encodeBytes(byte[])
*/
public final static byte[] decodeBytes(String str) throws IOException {
try {
ByteArrayInputStream encodedStringStream = new ByteArrayInputStream(str.getBytes());
InputStream decoder = MimeUtility.decode(encodedStringStream, "base64");
ByteArrayOutputStream decodedByteStream = new ByteArrayOutputStream();
byte[] buffer = new byte[256]; while (true) {
int read = decoder.read(buffer); if (read == -1) {
break;
} decodedByteStream.write(buffer, 0, read);
} decodedByteStream.flush(); return decodedByteStream.toByteArray();
} catch (MessagingException me) {
throw new IOException("Cannot decode data.");
}
} /**
* Encode binary data into String using base64.
*
* @see #decodeBytes(java.lang.String)
*/
public final static String encodeBytes(byte[] data) throws IOException {
try {
ByteArrayOutputStream encodedByteStream = new ByteArrayOutputStream();
OutputStream encoder = MimeUtility.encode(encodedByteStream, "base64");
encoder.write(data);
encoder.flush(); return new String(encodedByteStream.toByteArray());
} catch (MessagingException me) {
throw new IOException("Cannot encode data.");
}
} /**
* Verify that the given string is a valid email address.
* "Validity" in this context only means that the address conforms
* to the correct syntax (not if the address actually exists).
*
* @param email The email address to verify.
* @return a boolean indicating whether the email address is correctly formatted.
*/
public final static boolean verifyEmail(String email) {
if (email == null) {
return false;
} if (email.indexOf('@') < 1) {
return false;
} try {
new InternetAddress(email); return true;
} catch (AddressException e) {
return false;
}
}
}