jQuery判断邮箱格式对错实例代码讲解

废话不多说了,具体代码如下所示;

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>检测Email地址格式是否正确</title>
   <script src="http://apps.bdimg.com/libs/jquery/1.10.2/jquery.min.js"></script>
</head>
<body>
  <input type="text" id="emailname">
  <input type="submit" value="检测Email地址格式是否正确" class="is-email">
  <script type="text/javascript">
  $(".is-email").click(function(){
    var email = $("#emailname").val();
    if(email == ''){
       alert("请输入您的邮箱");
       return;
    }else if(email != "") {
      var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
      isok= reg.test(email );
      if (!isok) {
        alert("邮箱格式不正确,请重新输入!");
        return false;
      }
    };
    alert("ok 输入正确");
  });
  </script>
</body>
</html>

相关推荐