Java使用正则表达式验证手机号和电话号码的方法

网友投稿 288 2023-01-17


Java使用正则表达式验证手机号和电话号码的方法

一个朋友需要,所以写了这两个,话不多说,看代码

中国电信号段 133、149、153、173、177、180、181、189、199

中国联通号段 130、131、132、145、155、156、166、175、176、185、186

中国移动号段 134(0-8)、135、136、137、138、139、147、150、151、152、157、158、159、178、182、183、184、187、188、198

其他号段

14号段以前为上网卡专属号段,如中国联通的是145,中国移动的是147等等。

虚拟运营商

电信:1700、1701、1702

移动:1703、1705、1706

联通:1704、1707、1708、1709、171

卫星通信:1349

/**

* 获取当前的httpSession

* @return

*/

public static HttpSession getSession() {

return getRequest().getSession();

}

/**

* 手机号验证

* @param str

* @return 验证通过返回true

*/

public static boolean isMobile(final String str) {

Pattern p = null;

Matcher m = null;

boolean b = false;

p = Pattern.compile("^[1][3,4,5,7,8][0-9]{9}$"); // 验证手机号

m = p.matcher(str);

b = m.matches();

return b;

}

/**

* 电话号码验证

* @param str

* @return 验证通过返回true

*/

public static boolean isPhone(final 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;

}

public static void main(String[] args) {

String phone = "13900442200";

String phone2 = "021-88889999";

String phone3 = "88889999";

String phone4 = "1111111111";

//测试1

if(isPhone(phone) || isMobile(phone)){

System.out.println("1这是符合的");

}

//测试2

if(isPhone(phone2) || isMobile(phonhttp://e2)){

System.out.println("2这是符合的");

}

//测试3

if(isPhone(phone3) || isMobile(phone3)){

System.outYuGTJoNlt.println("3这是符合的");

}

//测试4

if(isPhone(phone4) || isMobile(phone4)){

System.out.println("4这是符合的");

}else{

System.out.println("不符合");

}

}

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对我们的支持。如果你想了解更多相关内容请查看下面相关链接


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:内网api接口管理工具(内网怎么使用地图api)
下一篇:spring 使用RabbitMQ进行消息传递的示例代码
相关文章

 发表评论

暂时没有评论,来抢沙发吧~