正则表达式验算手机号
时间: 2023-08-09 14:07:56 浏览: 64
在JavaScript中,可以使用正则表达式来验证手机号码。一个常见的正则表达式是:/^(1[3456789]\d{9})$/。这个正则表达式可以验证以1开头的11位数字,其中第二位可以是3、4、5、6、7、8、9中的任意一个数字。在JavaScript中,可以使用test()方法来验证手机号码是否符合这个正则表达式,例如:
```javascript
var phoneNumber = "13693347897";
var regex = /^(1[3456789]\d{9})$/;
var isValid = regex.test(phoneNumber);
console.log(isValid); // 输出true
```
在Java中,也可以使用正则表达式来验证手机号码。一个常见的正则表达式是:"^(1[3456789]\\d{9})$"。可以使用Pattern和Matcher类来进行验证,例如:
```java
import java.util.regex.*;
public class Main {
public static void main(String[] args) {
String phoneNumber = "13693347897";
String regex = "^(1[3456789]\\d{9})$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(phoneNumber);
boolean isValid = matcher.matches();
System.out.println(isValid); // 输出true
}
}
```
以上是两种常见的验证手机号码的正则表达式和使用方法。
相关推荐
![json](https://img-home.csdnimg.cn/images/20210720083646.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)