- /**
- * 手機號驗證
- *
- * @param str
- * @return 驗證通過返回true
- */
- public static boolean isMobile(String str) {
- Pattern p = null;
- Matcher m = null;
- boolean b = false;
- p = Pattern.compile("^[1][3,4,5,8][0-9]{9}$"); // 驗證手機號
- m = p.matcher(str);
- b = m.matches();
- return b;
- }
- /**
- * 電話號碼驗證
- *
- * @param str
- * @return 驗證通過返回true
- */
- public static boolean isPhone(String str) {
- Pattern p1 = null,p2 = null;
- Matcher m = null;
- boolean b = false;
- p1 = Pattern.compile("^[0][1-9]{2,3}-[0-9]{5,10}$"); // 驗證帶區號的
- p2 = Pattern.compile("^[1-9]{1}[0-9]{5,8}$"); // 驗證沒有區號的
- if(str.length() >9)
- { m = p1.matcher(str);
- b = m.matches();
- }else{
- m = p2.matcher(str);
- b = m.matches();
- }
- return b;
- }
-
點擊打開鏈接
Java電話號碼和手機號碼正則驗證
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.