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 }
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。