In our normal development process, we often have to judge a mobile phone number. I remember when I first started doing the front-end “cut-up”, I was wondering if this was back-end processing. In fact, if the front end determines whether the phone number is correct in advance, it can reduce the number of back-end requests and save broadband resources.

We need to understand the rules of mobile phone numbers first:

China Telecom number segment: 133, 149, 153, 173, 177, 180, 181, 189, 191, 199

China Unicom: 130, 131, 132, 145, 155, 156, 166, 171, 175, 176, 185, 186

China Mobile Segment: 134(0-8), 135, 136, 137, 138, 139, 147, 150, 151, 152, 157, 158, 159, 172, 178, 182, 183, 184, 187, 188, 198

Other number segment: 14 number segment used to be exclusive number segment of the network card, such as China Unicom is 145, China mobile is 147 and so on.

Virtual operator

Telecommunication: 1700, 1701, 1702, 162

Moves: 1703, 1705, 1706, 165

Unicom: 1704, 1707, 1708, 1709, 171, 167

Satellite Communication: 1349

First of all, mobile phone numbers can only be round numbers. We can judge it as follows:

function isMobile (mobile) {
  return /\d+/.test(mobile)
}
Copy the code

Then the phone number must start with a number and end with a number.

function isMobile (mobile) {
  return /^\d+$/.test(mobile)
}
Copy the code

We know that all mobile phone numbers start with a 1.

function isMobile (mobile) {
  return /^1\d+$/.test(mobile)
}
Copy the code

In addition, mobile phone numbers are 11 digits:

function isMobile (mobile) {
  return /^1\d{10}$/.test(mobile)
}
Copy the code

The second digit of the mobile phone number is 3 to 9:

function isMobile (mobile) {
  return /^1[3-9]\d{9}$/.test(mobile)
}
Copy the code

End, scatter flowers.