StringBuffer invaldMessage = new StringBuffer(""); regex = "^[a-zA-Z0-9_]+@([a-zA-Z0-9][a-zA-Z0-9]+.([a-zA-Z0-9.]*)[a-zA-Z]{2,5})$"; p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE); m = p.matcher(email);
if (stuff) { if (!m.find()) { return false; } } else if (!email.equals("") || email != "") { if (!m.find()) { return false; } } return true; }
只检验: 含有@,以".edu" or ".com"结束条件的简单 js <SCRIPT language="javascript"> function validate() { var name = document.forms[0].name.value, emailAddress = document.forms[0].comments.value, errorMsg="", errorDetcted = false; if(name == "" || emailAddress == "") { errorMsg += "Please fill in all fields!"; errorDetected = true; } if(!isEmailAddressValid(emailAddress)) { if(errorMsg.length>0)errorMsg += "\n"; errorMsg += "Email Address must contain @ and " + " end in .com or .edu "; errorDetected = true; } if(errorDetected)alert(errorMsg); return !errorDetected; } function isEmailAddressValid(s) { var atSign = new RegExp(".*(@).*"), dotEdu = new RegExp(".edu$"), dotCom = new RegExp(".com$"); return atSign.test(s) && ( dotEdu.test(s) || dotCom.test(s) ); } </SCRIPT>
import java.util.regex.*;
public class test{
public static void main(String[] args){
try{
String s = "";
while(!s.equals("q")){
System.out.print("input:");
DataInputStream in = new DataInputStream(new BufferedInputStream(System.in));
s = in.readLine();
System.out.println("your input is :"+s);
String check = "^([a-z0-9A-Z]+[-|\\._]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
Pattern regex = Pattern.compile(check);
Matcher matcher = regex.matcher(s);
boolean isMatched = matcher.matches();
if(isMatched){
System.out.println("it's a email");
}else{
System.out.println("it's not a email");
}
}
}catch(Exception e){
System.out.println("error"+e.getMessage());
}
}
}
偶觉得client和server都要验证。。client的js验证很容易绕过。。to: 楼主 /**
* 检测用户提交的email格式是否正确
*
* @param email 用户提交的email值
* @param stuff 是否必须填写,为true时必须填写,为flase时可不必填写
* @return boolean 如用户输入格式正确,则返回true,否则返回false
* @since 1.0
*/
public boolean checkEmail(String email, boolean stuff) {
StringBuffer invaldMessage = new StringBuffer("");
regex = "^[a-zA-Z0-9_]+@([a-zA-Z0-9][a-zA-Z0-9]+.([a-zA-Z0-9.]*)[a-zA-Z]{2,5})$";
p = Pattern.compile(regex, Pattern.CASE_INSENSITIVE);
m = p.matcher(email);
if (stuff) {
if (!m.find()) {
return false;
}
}
else if (!email.equals("") || email != "") {
if (!m.find()) {
return false;
}
}
return true;
}
function validate()
{
var name = document.forms[0].name.value,
emailAddress = document.forms[0].comments.value,
errorMsg="",
errorDetcted = false;
if(name == "" || emailAddress == "")
{
errorMsg += "Please fill in all fields!";
errorDetected = true;
}
if(!isEmailAddressValid(emailAddress))
{
if(errorMsg.length>0)errorMsg += "\n";
errorMsg += "Email Address must contain @ and " + " end in .com or .edu ";
errorDetected = true;
}
if(errorDetected)alert(errorMsg);
return !errorDetected;
}
function isEmailAddressValid(s)
{
var atSign = new RegExp(".*(@).*"),
dotEdu = new RegExp(".edu$"),
dotCom = new RegExp(".com$");
return atSign.test(s) && ( dotEdu.test(s) || dotCom.test(s) );
}
</SCRIPT>
public static void main(String[] arg){
String email1="@";
String email2=".";
String email3="[email protected]";
if(email3.subString(email1)&&email3.subString(email2))
System.out.println(email3+" :::is email address");
else{
System.out.println(email3 + "is not email address ") ;
}
}
}
http://community.csdn.net/Expert/topic/4320/4320046.xml?temp=.1445124
public static void main(String[] arg){
String email1="@";
String email2=".";
String email3="[email protected]";
if((email3.indexof(email1)!=-1)&&(email3.indexof(email2)!=-1))
System.out.println(email3+" :::is email address");
else{
System.out.println(email3 + "is not email address ") ;
}
}
}
更正如下
private static Pattern p=null;
public static boolean validateEmail(String mail){
if(mail == null)
return false;
if(p == null)
p = Pattern.compile("^[_a-zA-Z0-9]+(\\.[_a-zA-Z0-9]+)*@[a-zA-Z0-9_-]+(\\.[a-z0-9A-Z-_]+)+$");
Matcher m= p.matcher(mail);
return m.matches();
}
/**
* @param args
*/
public static void main(String[] args) {
System.out.println(validateEmail("[email protected]"));
}}