在js中实现邮箱格式的验证

时间:2021-07-04 08:09:41

在js中实现邮箱格式的验证

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>在此处插入标题</title>
<script type="text/javascript">
//判断用户输入的电子邮箱格式是否正确
function checkEmail(){
  var myforms=document.forms;
  var myemail=myforms[0].email.value;
  var myReg=/^[a-zA-Z0-9_-]+@([a-zA-Z0-9]+\.)+(com|cn|net|org)$/;

  if(myReg.test(myemail)){
    return true;
  }else{
    myspan.innerText="邮箱格式不对!";
    return false;
}
}

</script>
</head>
<body>
  <form action="#">
    <table>
    <tr><td>邮箱:<input type="text" id=email></td><td><span id=myspan style="text-size:18pt;color:red;"></span></td></tr>
    <tr><td><input type="submit" value="提交" onclick="return checkEmail()"/></td><td></td></tr>
    </table>
  </form>

</body>
</html>