Home Web Front-end JS Tutorial js combined with regular expressions to implement domestic mobile phone number segment verification_javascript skills

js combined with regular expressions to implement domestic mobile phone number segment verification_javascript skills

May 16, 2016 pm 03:53 PM
js Phone number regular

Attach a utils object, which contains a function to verify mobile phone number and a function to format and return data

var isChinaMobile = /^134[0-8]\d{7}$|^(?:13[5-9]|147|15[0-27-9]|178|18[2-478])\d{8}$/; //移动方面最新答复
var isChinaUnion = /^(?:13[0-2]|145|15[56]|176|18[56])\d{8}$/; //向联通微博确认并未回复
var isChinaTelcom = /^(?:133|153|177|18[019])\d{8}$/; //1349号段 电信方面没给出答复,视作不存在
var isOtherTelphone  = /^170([059])\d{7}$/;//其他运营商
 
var utils = {
  checkMobile: function(telphone){
    telphone = this.trim(telphone);
    if(telphone.length !== 11){
      return this.setReturnJson(false, '未检测到正确的手机号码');
    }
    else{
      if(isChinaMobile.test(telphone)){
        return this.setReturnJson(true, '移动', {name: 'ChinaMobile'});
      }
      else if(isChinaUnion.test(telphone)){
        return this.setReturnJson(true, '联通', {name: 'ChinaUnion'});
      }
      else if(isChinaTelcom.test(telphone)){
        return this.setReturnJson(true, '电信', {name: 'ChinaTelcom'});
      }
      else if(isOtherTelphone.test(telphone)){
        var num = isOtherTelphone.exec(telphone);
        return this.setReturnJson(true, '', {name: ''});
      }
      else{
        return this.setReturnJson(false, '未检测到正确的手机号码');
      }
    }
  },
  setReturnJson: function(status, msg, data){
    if(typeof status !== 'boolean' && typeof status !== 'number'){
      status = false;
    }
    if(typeof msg !== 'string'){
      msg = '';
    }
    return {
      'status': status,
      'msg': msg,
      'data': data
    };
  }
}
Copy after login

Verify mobile phone numbers in the 130-139, 150-159, 180-189 range

<script type="text/javascript">
var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
if(!myreg.test($("#phone").val())) 
{ 
  alert('请输入有效的手机号码!'); 
  return false; 
} 
</script>
Copy after login

The above code is debugged under jquery.

No jquery code required

function validatemobile(mobile) 
  { 
    if(mobile.length==0) 
    { 
     alert('请输入手机号码!'); 
     document.form1.mobile.focus(); 
     return false; 
    }   
    if(mobile.length!=11) 
    { 
      alert('请输入有效的手机号码!'); 
      document.form1.mobile.focus(); 
      return false; 
    } 
    
    var myreg = /^(((13[0-9]{1})|(15[0-9]{1})|(18[0-9]{1}))+\d{8})$/; 
    if(!myreg.test(mobile)) 
    { 
      alert('请输入有效的手机号码!'); 
      document.form1.mobile.focus(); 
      return false; 
    } 
  } 
Copy after login

The above is the entire content of this article, I hope you all like it.

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use Xiaohongshu account to find users? Can I find my mobile phone number? How to use Xiaohongshu account to find users? Can I find my mobile phone number? Mar 22, 2024 am 08:40 AM

With the rapid development of social media, Xiaohongshu has become one of the most popular social platforms. Users can create a Xiaohongshu account to show their personal identity and communicate and interact with other users. If you need to find a user’s Xiaohongshu number, you can follow these simple steps. 1. How to use Xiaohongshu account to find users? 1. Open the Xiaohongshu APP, click the &quot;Discover&quot; button in the lower right corner, and then select the &quot;Notes&quot; option. 2. In the note list, find the note posted by the user you want to find. Click to enter the note details page. 3. On the note details page, click the &quot;Follow&quot; button below the user's avatar to enter the user's personal homepage. 4. In the upper right corner of the user's personal homepage, click the three-dot button and select &quot;Personal Information&quot;

How to set up if WeChat does not allow adding by mobile phone number How to set up if WeChat does not allow adding by mobile phone number Mar 12, 2024 pm 05:04 PM

How to set up WeChat to disallow adding via mobile phone number? In WeChat APP, you can disallow adding WeChat via mobile phone number, but most users don’t know how to set it up to not allow WeChat to be added via mobile phone number. Next is WeChat brought by the editor to users It is not allowed to add setting method tutorials through mobile phone numbers. Interested users can come and take a look! Tutorial on how to use WeChat. How to set up WeChat when adding a mobile phone number is not allowed. 1. First open the WeChat APP, enter the main page, click [Me] in the lower right corner to enter, and select the [Settings] function; 2. Then enter the setting function page and find [Friend Permissions] ] Service function; 3. Then on the friend permissions interface, slide the button behind [Verification is required when adding me as a friend]; 4. Finally, on the page shown below, slide [Mobile Phone Number]

How to change the mobile phone number of Xiaohongshu account? Will there be any impact if I change my mobile phone number? How to change the mobile phone number of Xiaohongshu account? Will there be any impact if I change my mobile phone number? Mar 22, 2024 am 08:11 AM

In the digital age, Xiaohongshu has become a platform for many people to share their lives and discover good things. Over time, we may need to change the mobile phone number on the account. So, how to change Xiaohongshu’s mobile phone number? 1. How to change the mobile phone number of Xiaohongshu account? 1. Log in to your Xiaohongshu account and enter the “My” page. 2. Click &quot;...&quot; in the upper right corner and select &quot;Settings&quot;. 3. On the settings page, find the &quot;Account and Security&quot; option and click to enter. 4. On the Account and Security page, find the &quot;Mobile Phone Number&quot; option and click to enter. 5. At this time, the page will display the current mobile phone number, click &quot;Change Mobile Number&quot;. 6. Enter the new mobile phone number, receive the verification code, and fill it out. 7. Set a new password to ensure account security. 8. After completing the above steps,

Recommended: Excellent JS open source face detection and recognition project Recommended: Excellent JS open source face detection and recognition project Apr 03, 2024 am 11:55 AM

Face detection and recognition technology is already a relatively mature and widely used technology. Currently, the most widely used Internet application language is JS. Implementing face detection and recognition on the Web front-end has advantages and disadvantages compared to back-end face recognition. Advantages include reducing network interaction and real-time recognition, which greatly shortens user waiting time and improves user experience; disadvantages include: being limited by model size, the accuracy is also limited. How to use js to implement face detection on the web? In order to implement face recognition on the Web, you need to be familiar with related programming languages ​​and technologies, such as JavaScript, HTML, CSS, WebRTC, etc. At the same time, you also need to master relevant computer vision and artificial intelligence technologies. It is worth noting that due to the design of the Web side

How to create a stock candlestick chart using PHP and JS How to create a stock candlestick chart using PHP and JS Dec 17, 2023 am 08:08 AM

How to use PHP and JS to create a stock candle chart. A stock candle chart is a common technical analysis graphic in the stock market. It helps investors understand stocks more intuitively by drawing data such as the opening price, closing price, highest price and lowest price of the stock. price fluctuations. This article will teach you how to create stock candle charts using PHP and JS, with specific code examples. 1. Preparation Before starting, we need to prepare the following environment: 1. A server running PHP 2. A browser that supports HTML5 and Canvas 3

Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Essential tools for stock analysis: Learn the steps to draw candle charts with PHP and JS Dec 17, 2023 pm 06:55 PM

Essential tools for stock analysis: Learn the steps to draw candle charts in PHP and JS. Specific code examples are required. With the rapid development of the Internet and technology, stock trading has become one of the important ways for many investors. Stock analysis is an important part of investor decision-making, and candle charts are widely used in technical analysis. Learning how to draw candle charts using PHP and JS will provide investors with more intuitive information to help them make better decisions. A candlestick chart is a technical chart that displays stock prices in the form of candlesticks. It shows the stock price

How to register two B-site numbers with one mobile phone number? How does Station B unbind its mobile phone? How to register two B-site numbers with one mobile phone number? How does Station B unbind its mobile phone? Mar 21, 2024 pm 10:10 PM

Bilibili (Bilibili), as a video sharing website very popular among Chinese young people, has attracted a large number of users. Some users hope to have two Bilibili accounts so that they can be managed and used separately. So, how to register two B-site numbers with one mobile phone number? This article will focus on this issue and how to unbind the mobile phone. 1. How to register two B-site numbers with one mobile phone number? 1. Register a new account: First, open the Bilibili App on your mobile phone or log in to the official website, click the &quot;Register&quot; button, and select the registration method. You can use your mobile phone number, email or third-party account (such as WeChat, QQ, etc.) to register. 2. When registering an account, please fill in the necessary information according to the system prompts, including mobile phone number, verification code, and set password. Be sure to use different accounts

What should I do if my railway 12306 mobile phone number has been changed and cannot be verified? What should I do if my railway 12306 mobile phone number has been changed and cannot be verified? Feb 28, 2024 am 09:00 AM

In recent years, the various certification rules of Railway 12306 have been revised to make them more standardized. In the past, when you could buy tickets through other people's mobile phone numbers or old mobile phone numbers, now when buying tickets, you will be prompted for unauthenticated operations, which affects users' travel. So how to change the mobile phone number in 12306? This tutorial guide will bring you a detailed guide introduction, hoping to help everyone in need. Then the first choice is to click on the modify mobile phone number function when you have logged in to 12306app, then enter the modification page, click to bind a new mobile phone number, use the new mobile phone number to send a text message, then enter the text message editing page, follow the prompts to send the edit Please fill in the verification code received and enter it into 12306app to complete the modification.

See all articles