Home >Web Front-end >Front-end Q&A >How to verify mobile phone number with javascript
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.
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) }
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) }
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) }
In addition, the mobile phone numbers are all 11 digits:
function isMobile (mobile) { return /^1\d{10}$/.test(mobile) }
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) }
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!