Home > Web Front-end > JS Tutorial > Detailed explanation of regular verification of mobile phone number (with code)

Detailed explanation of regular verification of mobile phone number (with code)

php中世界最好的语言
Release: 2018-03-29 17:54:23
Original
1994 people have browsed it

This time I will bring you a detailed explanation of the regular verification of mobile phone numbers (with code). What are the precautions for regular verification of mobile phone numbers? The following is a practical case, let's take a look.

Only numbers allowed

<xsl:attribute name="onkeyup">value=value.replace(/[^\d]/g,'')</xsl:attribute>
<xsl:attribute name="onbeforepaste">clipboardData.setData('text',clipboardData.getData('text').replace(/[^\d]/g,''))</xsl:attribute>
Copy after login
//固定电话只要有一个填写即可
$('.form-inline').submit(function(){
    var m = $('input[name=mobile]').val();
    var p = $('input[name=phone]').val();
    var reg = /^1\d{10}$/;
    if((m == "" || !reg.test(m)) && p == ""){
      $('input[name=mobile]').addClass('error_color');
      $('input[name=mobile]').tooltip('show');
      return false;
    }else{
      $('input[name=mobile]').removeClass('error_color');
      $('input[name=mobile]').tooltip('hide');
      return true; 
    }
  })
Copy after login

Let’s take a look at the latest mobile phone number verificationRegular expression

Due to the continuous updating of mobile phone number segments, the previous regular expressions can no longer meet the needs. Rewrite this expression, the number segment data source is based on: http://www.jihaoba.com/tools/haoduan/

Existing mobile phone number segment:

手机:139 138 137 136 135 134 147 150 151 152 157 158 159 178 182 183 184 187 188

China Unicom: 13 0 131 132 155 156 185 186 145 176
Telecom: 133 153 177 173 180 181 189

Virtual operator:

170 171

After sorting:

130~139 145,147 15[012356789 ] 178,176,177,173,170,171 180~189

var regex = {
  mobile: /^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/
}
Copy after login

js:

var bool = checkRegexp(jq("#mobile"), /^0?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/, "电话格式不对");
Copy after login

Expression analysis:

"/" represents a regular expression Mode.

"^" represents the starting position of the

string , and "$" represents the end position of the string.

"?" means matching one or zero of the previous characters, so 0? here means that the mobile phone number can start with 0 or not.

The next part verifies the 11-digit mobile phone number, starting from 13, because it ranges from 130-139, so the optional range is [0-9], the number starting with 15 does not have 154, so there is no 154 in [] Without the number 4, of course it can also be written as [0-35-9]. The following numbers 18 and 14 are the same as above.

The parentheses enclosed represent a subexpression, which contains 4 optional branches separated by "|". In the regular expression, the

priority of "|" is The lowest, each branch here matches 3 characters (a [] can only match one character, which means optional), that is, the first 3 digits of the mobile phone number, then there are 8 digits needed afterward. Matching can be any character from 0 to 9, so it is "[0-9]{8}". The number in {} represents the number of matching previous characters.

Analysis completed.

What if a big-headed ghost writes something like 86,17951 on the front?

/^(0|86|17951)?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$/
Copy after login

C# version:

using System.Text.RegularExpressions;
    public static bool IsTelephone(string str_telephone)
    {
      return Regex.IsMatch(str_telephone, @"^(0|86|17951)?(13[0-9]|15[012356789]|17[013678]|18[0-9]|14[57])[0-9]{8}$");
    }
Copy after login
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 using regular expressions to extract strings (with code)

Using regular expressions in How to find letters and numbers in js

The above is the detailed content of Detailed explanation of regular verification of mobile phone number (with code). 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