Home  >  Article  >  Web Front-end  >  A simple example of using javascript to determine a phone number

A simple example of using javascript to determine a phone number

黄舟
黄舟Original
2017-05-25 09:19:361868browse

This article mainly introduces the relevant information of javascriptJudge the phone number example written simply. Friends in need can refer to

Judge the phone number simply written in javascript

When registering on many websites, we are required to fill in the phone number. I originally wanted to fool it, but it didn't work. It kept prompting that it was incorrect. I searched a lot online, regular expression, I found a lot of mistakes,

Finally I wrote a simple but more practical one

First is the content of the html part

手机号:<input type="text" class="loginuser" placeholder="请输入您的手机号" id="uid" onblur="checkPhone()" />
        <span id="uidt" style="margin-left: 100px"></span>

The content in the span tag It is mainly used to write prompts. For example, if you enter it incorrectly, you will be prompted "Please enter the correct mobile phone number." If you enter it correctly, you will be prompted "OK";

Then comes the javascript part

 function checkPhone(){
    var phone = document.getElementById(&#39;uid&#39;).value;
    if(!(/^1[34578]\d{9}$/.test(phone))){
      return document.getElementById(&#39;uidt&#39;).innerHTML = &#39;请输入正确的手机号&#39;;
      return false;
    }    else{return document.getElementById(&#39;uidt&#39;).innerHTML = &#39;ok&#39;;}
  }

Such a simple mobile phone number verification is completed

The above is the detailed content of A simple example of using javascript to determine a phone number. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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