Java验证手机号和邮箱

代码如下:

 1 public static boolean isEmail(String email) {
 2         String str = "^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
 3         Pattern p = Pattern.compile(str);
 4         Matcher m = p.matcher(email);
 5         return m.matches();
 6         }
 7     
 8     public static boolean isPhoneNumberValid(String phoneNumber) {
 9         boolean isValid = false;
10 
11         String expression = "((^(13|15|18)[0-9]{9}$)|(^0[1,2]{1}\d{1}-?\d{8}$)|(^0[3-9] {1}\d{2}-?\d{7,8}$)|(^0[1,2]{1}\d{1}-?\d{8}-(\d{1,4})$)|(^0[3-9]{1}\d{2}-? \d{7,8}-(\d{1,4})$))";
12         CharSequence inputStr = phoneNumber;
13 
14         Pattern pattern = Pattern.compile(expression);
15         Matcher matcher = pattern.matcher(inputStr);
16 
17         if (matcher.matches()) {
18             isValid = true;
19         }
20 
21             return isValid;
22     }
原文地址:https://www.cnblogs.com/zqh005/p/4271999.html