js regular expression to detect whether the email address is correct

The general email format is: [email protected]. If you use indexOf to judge the email address, then when the user enters 123456@qq, the detection can still pass. Obviously, this address lacks the following ".com", so This is not possible, it is better to use regular expressions to judge:


<script type="text/javascript">
function ischeckemail(){
var email = document.getElementById("emailname").value;
  if(email != "") {
     var reg = /^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]+$/;
     //调用正则验证test()函数
     isok= reg.test(email);
       if(!isok) {
            alert("邮箱格式不正确,请重新输入!");
            document.getElementById("emailname").focus();
            return false;
        }
    };
}
</script>

<input type="text" id="emailname">
<input type="submit" value="检测Email地址格式是否正确" click="return ischeckemail()">

Guess you like

Origin blog.csdn.net/m0_59735348/article/details/119937450