java正则表达式校验手机号、邮箱、身份证

1.校验手机号
/** 
 * 验证手机号码   
 * @param mobiles   
 * @return     
 */
  public static boolean isMobileNO(  String mobiles){
    boolean flag = false;    
   try{    
    Pattern regex = Pattern.compile("^((13[0-9])|(15[^4,\\D])|(17[0-9])|(18[0,5-9]))\\d{8}$");    
    Matcher m = regex .matcher(mobiles);    
    flag = m.matches();    
   }catch(Exception e){    
    flag = false;    
   }    
   return flag;
  }

2.校验邮箱
/** 
 * 验证邮箱地址是否正确   
 * @param email   
 * @return   
 */
  public static boolean checkEmail(  String email){
    boolean flag = false;    
	   try{    
	    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);    
	    flag = matcher.matches();    
	   }catch(Exception e){    
	    flag = false;    
	   }    
	   return flag;
  }


3.校验身份证号码
/**
     * 校验身份证
     * 
     * @param idCard
     * @return 校验通过返回true,否则返回false
     */
    public boolean isIDCard(String idCard) {
        String REGEX_ID_CARD = "(^\\d{18}$)|(^\\d{15}$)";
        return Pattern.matches(REGEX_ID_CARD, idCard);
    }



你可能感兴趣的:(Java)