This time I will bring you the js mobile phone number verification that is prone to errors. What are the precautions for js mobile phone number verification that is prone to errors? The following is a practical case, let’s take a look.
Write a simple regular expression to verify the 11-digit mobile phone number. The beginning is allowed to be 13, 15, and 18. I wrote it like this at the beginning:
var reg = /^(13[0-9]{9})|(15[0-9]{9})|(18[0-9]{9})$/;
Run it and find that even if it is 13988888877157777, can also pass the verification, which means that this writing method is wrong. My original intention is to like this:
^(13[0-9]{9})$ 或者 ^(15[0-9]{9})$ 或者 ^(18[0-9]{9})$
So the correct writing method is: var reg = /^1[358][0-9] {9}$/;
This is how to ensure 11 digits
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
Detailed explanation of the use of lastIndex in regular expressions
Detailed explanation of the use of balance groups in regular expressions (with code)
The above is the detailed content of Error-prone js mobile phone number verification. For more information, please follow other related articles on the PHP Chinese website!