最近做一個Android App的報名功能,需要在手機端對信息就進行有效性判斷:
1.手機號的判斷
目前手機前兩位:13,14,15,17,18
public static boolean isMobileNO(String mobiles) { if(TextUtils.isEmpty(mobiles)){ return false; } //1[3|4|5|7|8][0-9]表示以1開頭,后跟3,4,5,7,8,[0-9]表示數字即可,\d{8}剩余八位填充隨意數字 Pattern p = Pattern.compile("^1[3|4|5|7|8][0-9]\\d{8}$"); Matcher m = p.matcher(mobiles); return m.matches(); }
2.QQ號或者微信號判斷
如果要細分微信號還是QQ號,需要再細分,考慮的更多些,比如:微信號也涉及到手機號或QQ號即微信號的情況。項目只需要判斷是有效的QQ號或者是微信號即可
public static boolean isQQOrWX(String qqorwx) { if(TextUtils.isEmpty(qqorwx)){ return false; } //QQ號最短5位,微信號最短是6位最長20位 Pattern p = Pattern.compile("^[a-zA-Z0-9_-]{5,19}$"); Matcher m = p.matcher(qqorwx); return m.matches(); }
3.姓名有效性判斷
項目只需要判斷是中文,還是英文,或中文+英文,過濾掉其他特殊字符或表情等等
public static boolean isName(String name){ if(TextUtils.isEmpty(name)){ return false; } //因項目需求,只需要限定在中文和英文上即可,長度已經在Android EditText中限制輸入,此處不做長度限制 Pattern p = Pattern.compile("^[\u4E00-\u9FA5a-zA-Z]+"); Matcher m = p.matcher(name); return m.matches(); }