java验证电话手机

2026-02-14 17:15:50

1、打开MyEclipse,然后新建类“Test”

java验证电话手机

2、在Test类中,编写验证电话号码的方法,代码:

/**

* 判断是否是电话

* @param phone

* @return

*/

public static boolean isPhone(String phone) {

Pattern phonePattern = Pattern.compile("^1\\d{10}$");

Matcher matcher = phonePattern.matcher(phone);

if(matcher.find()){

return true;

}

return false;

}

java验证电话手机

3、编写测试方法mian,代码如下:

public static void main(String[] args) {

Test t = new Test();

System.out.println(t.isPhone("18710622658"));

}

java验证电话手机

4、运行mian方法结果:true

java验证电话手机

猜你喜欢