Java驗證電子郵箱地址、電話號碼

demo1:

/**
 * 本類用於 處理、檢查導入數據的格式
 * @author Administrator
 * weiwenshuai 2011 09 15
 */
public class CheckData {
 
 /**
  * 驗證手機號碼、電話號碼是否有效
  *  手機號前面加86的情況也考慮
  * 新聯通  
    *(中國聯通+中國網通)手機號碼開頭數字 130、131、132、145、155、156、185、186
    * 新移動
    * (中國移動+中國鐵通)手機號碼開頭數字 134、135、136、137、138、139、147、150、151、152、157、158、159、182、183、187、188
    * 新電信
     * (中國電信 <http://baike.baidu.com/view/3214.htm>+中國衛通)手機號碼開頭數字 133、153、189、180
  * 座機:
  *3/4位區號(數字)+ “-” + 7/8位(數字)+ “-”+數字位數不限
  *說明:“-”+數字位數不限;這段可有可無
  */
 public static String checkphoto(String photo){ 
  if(null!=photo){
   String reisphoto=photo.replace(",",",").replace(";",",").replace(";",",").replace(" ", ",").replace(" ",",").replace("/",",")
   .replace("\\", ",");
   String[] photo1=reisphoto.split(",");
   String[] photo2=new String[photo1.length];
   boolean isfirst;
   if(null!=photo1&&photo1.length>0){
    for(int i=0;i<photo1.length;i++){
    isfirst=false;
    if(photo1[i].matches("(^[0-9]{3,4}-[0-9]{3,8}$)|^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|2|3|5|6|7|8|9])\\d{8}$")){
     photo2[i]=photo1[i];
     isfirst=true;
    }
    //第二規則 “-”+數字位數不限 和手機號前面加86的情況也考慮
    if(!isfirst){
     if(photo1[i].matches("(^[0-9]{3,4}-[0-9]{3,8}-[0-9]{0,100}$)|^((\\+86)|(86))?(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|2|3|5|6|7|8|9])\\d{8}$")){
      photo2[i]=photo1[i];
     }
    }
    }
    //如果兩個電話 只用一個
    if(photo2.length>0){
     return photo2[0];
    }
   }
  }  
  return null;
 }
 public static void main(String[] args){
  String[] photo =new String[]{"1523620111","15811363254 15811364216","15811364216","13011111111,15811364216","022-6232903-22","022-6232903","+8615811364216","8615811224181"};
  for(int i=0;i<photo.length;i++){
   System.out.println(CheckData.checkphoto(photo[i]));
  }
  
 }
}

 

demo2:

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * <p>
 *
 * <p>Copyright the original author or authors.
 *
 * @author Liu Huibin
 * @date Aug 27, 2010
 * @dateLastModified Aug 27, 2010
 */
public class Test {
public static void main(String[] args) {

//電子郵件
 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("[email protected]");
 boolean isMatched = matcher.matches();
 System.out.println(isMatched);

 

/* 電話號碼

String check = "^(13[4,5,6,7,8,9]|15[0,8,9,1,7]|188|187)\\d{8}$";
 Pattern regex = Pattern.compile(check);
 Matcher matcher = regex.matcher("13555655606");
 boolean isMatched = matcher.matches();
 System.out.println(isMatched);

*/
}
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章