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

js phone number verification method

PHPz
PHPzforward
2016-05-16 15:37:452421browse

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>

Home phone number JS verification:

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

Statement:
This article is reproduced at:jb51.net. If there is any infringement, please contact admin@php.cn delete