相信大家在項目中經常因為項目經理想出的各種奇葩需求而感到頭疼吧,當然了作為一名開發人員,沒辦法隻能硬着頭皮迎難而上。因為自己的項目中遇到了郵箱,手機号碼等的驗證需求,之後做了一下簡單的總結,具體實作代碼如下:
//判斷身份證的方法
public static boolean isIdNO(String IDNumber){
boolean result=IDNumber.matches(“[0-9]{15}|[0-9]{17}[0-9X,x]”);
if(result){
int year,month,date;
if(IDNumber.length()==15){
year=Integer.parseInt(IDNumber.substring(6,8));
month=Integer.parseInt(IDNumber.substring(8,10));
date=Integer.parseInt(IDNumber.substring(10,12));
}
else{
year=Integer.parseInt(IDNumber.substring(6,10));
month=Integer.parseInt(IDNumber.substring(10,12));
date=Integer.parseInt(IDNumber.substring(12,14));
}
switch(month){
case 2:result=(date>=1)&&(year%4==0?date<=29:date<=28);break;
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:result=(date>=1)&&(date<=31);break;
case 4:
case 6:
case 9:
case 11:result=(date>=1)&&(date<=31);break;
default:result=false;break;
}
}
return result;
}
//驗證郵箱的方法
public static boolean checkEmail(String email) {// 驗證郵箱的正規表達式
String format = “^([a-z0-9A-Z]+[-|\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\.)+[a-zA-Z]{2,}$”;
if (email.matches(format)) {
return true;// 郵箱名合法,傳回true
} else {
return false;// 郵箱名不合法,傳回false
}
}
public static boolean isMobileNO(String mobiles) {
String telRegex = “[1][1-9]\d{9}”;// “[1]”代表第1位為數字1,”[358]”代表第二位可以為3、5、8中的一個,”\d{9}”代表後面是可以是0~9的數字,有9位。
if (TextUtils.isEmpty(mobiles))
return false;
else
return mobiles.matches(telRegex);
}
public static boolean isChinese(char c) {
Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION
|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS) {
return true;
}
return false;
}
public static boolean checkNameChese(String name)
{
boolean res=true;
char [] cTemp = name.toCharArray();
for(int i=0;i