js验证email格式的几种方法

发布时间:2019-08-22编辑:脚本学堂
本文介绍js验证email格式的几种方法。

方法1:
 

复制代码 代码如下:

<html>
<head>
<title>Checking an email address - Version 01</title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingAddress(str){
    var myRegExp = /[a-z0-9-]{1,30}@[a-z0-9-]{1,65}.[a-z]{3}/ ;
    return myRegExp.test(str)
}

function TestGuess(){

var EmailAddr = "jbxue123@sina.com".toLowerCase();
alert(IsMatchingAddress(EmailAddr));
}
// -->
</script>
</head>
<body>
<h3>This page allows you to enter and check
 an email address such as jbxue123@sina.com</h3>
<form>
<button type="Button" onclick="TestGuess()">
Click here to enter email address</button>
</form>
</body>
</html>

方法2:
 

复制代码 代码如下:

<html>
<head>
<title>Checking an email address - Version 02</title>
<script type="text/javascript" language="javascript">
<!-- //
function IsMatchingAddress(str){
    var myRegExp = /[a-z0-9-.]{1,30}@[a-z0-9-]{1,65}.(com|net|org|info|biz|([a-z]{2,3}.[a-z]{2}))/ ;
    return myRegExp.test(str)
}

function TestGuess(){
    var EmailAddr = "jbxue123@sina.com".toLowerCase();
    alert(IsMatchingAddress(EmailAddr));
}
// -->
</script>
</head>
<body>
<h3>This page allows you to enter and check an email address
 such as jbxue123@sina.com,</h3>
<form>
<button type="Button" onclick="TestGuess()">
Click here to enter email address</button>
</form>
</body>
</html>

方法3:
 

复制代码 代码如下:

<html>
<head>
<title>E-mail Example</title>
<script type="text/javascript">
    function isValidEmail(sText) {
        var reEmail = /^(?:w+.?)*w+@(?:w+.)+w+$/;
        return reEmail.test(sText);
    }
    function validate() {
        var oInput1 = document.getElementById("txt1");
        if (isValidEmail(oInput1.value)) {
            alert("Valid");
        } else {
            alert("Invalid!");
        }

    }
</script>
</head>
<body>
    <P>E-mail Address: <input type="text" id="txt1" /><br />
    <input type="button" value="Validate" onclick="validate()" /></p>
</body>
</html>

##==>常用js验证代码大全(Email、手机号码、身份证号码等)