How to verify mobile phone number with javascript

藏色散人
Release: 2021-07-03 10:37:19
Original
4350 people have browsed it

Javascript method to verify mobile phone number: first create a js code file; then judge by regular expression "return /^1[3-9]\d{9}$/.test(mobile)" Just make sure the mobile phone number is correct.

How to verify mobile phone number with javascript

The operating environment of this article: windows7 system, javascript version 1.8.5, Dell G3 computer.

How to verify mobile phone number with javascript?

JavaScript mobile phone number regular expression writing method

In our daily development process, we often have to determine a mobile phone number. I remember when I first started working on the front-end "cutting picture", I had doubts. Isn't this handled by the back-end? In fact, if the front end determines whether the mobile phone number is correct in advance, it can reduce backend requests and save broadband resources.

We must first understand the rules of mobile phone numbers "from Baidu Encyclopedia":

China Telecom signal segments: 133, 149, 153, 173, 177, 180, 181, 189, 191, 199

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

China Mobile number 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 segments : Segment 14 used to be the exclusive number segment for Internet cards, such as China Unicom's 145, China Mobile's 147, etc.

Virtual Operator

Telecom: 1700, 1701, 1702, 162

Mobile: 1703, 1705, 1706, 165

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

Satellite communication: 1349

First of all, mobile phone numbers can only be integers. We can judge like this:

function isMobile (mobile) {
  return /\d+/.test(mobile)
}
Copy after login

Then the mobile phone number must start with a number and end with a number. The writing method can be upgraded:

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

The above regular rule can only make a simple judgment. We know all mobile phones The numbers all start with 1, and the writing can be upgraded:

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

In addition, the mobile phone numbers are all 11 digits:

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

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

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

Recommended study: "javascript Advanced Tutorial"

The above is the detailed content of How to verify mobile phone number with javascript. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template