Home > Web Front-end > JS Tutorial > js phone number verification method

js phone number verification method

PHPz
Release: 2018-09-28 17:39:15
forward
2425 people have browsed it

JS phone number verification is a relatively common type of verification. A small example of JavaScript phone number verification is given below. Domestic landline telephone numbers are composed of seven or eight digits, and may also have long-distance area codes.

A general home phone number without an area code is 7 or 8 digits long, and the area code is 3 or 4 digits and a dash. Therefore, when we write verification code, we can verify the phone number through two regular expressions.

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>电话号码验证</title>
  <script type="text/javascript">
    function checkTel() {
      var obj = document.getElementById("txtTel");
      var value = obj.value;
      var regTel1 = /^(([0\+]\d{2,3}-)?(0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/.test(value);//带区号的固定电话
      var regTel2 = /^(\d{7,8})(-(\d{3,}))?$/.test(value);//不带区号的固定电话
      if (value != "") {
        if (!regTel1 && !regTel2) { 
          alert("电话号码输入有误!");
          obj.focus();
          return false;
        }
      }
      else {
        alert("请输入电话号码!");
        return false;
      }
      alert("电话号码输入正确!");
      return true;
    }
  </script>
</head>
<body>
  请输入电话号码:
  <input type="text" id="txtTel" />
  <input type="button" id="btnCheck" value="验证" onclick="return checkTel();" />
</body>
</html>
Copy after login

Home phone number JS verification:

The above is the entire content of this chapter. For more related tutorials, please visit JavaScript Video Tutorial!

source:jb51.net
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