找了一个比较全的邮箱验证,代码如下:
<html>
<head>
<title>Untitled</title>
<meta http-equiv="content-type" content="text/html;charset=gb2312">
<script type="text/javascript" language="javascript">
/*有效的邮箱验证*/
function emailCheck(){
var theEmail=document.getElementById("valueEmail").value;
var Email = theEmail.toLowerCase();
var ok = false;
var count = 0;
var fullEmail;
if (Email.length!=0 && Email.indexOf('@') != -1 && Email.indexOf('.') != -1 && (Email.indexOf('.') - Email.indexOf('@') > 1)){
//开头不能有@和点 & 结尾不能有@和点
if ((Email.length-1) != Email.indexOf('@') && (Email.length-1) !=Email.indexOf('.') && Email.indexOf('@')!=0 && Email.indexOf('.')!=0){
for (var i=0;i<Email.length;i++){
fullEmail=Email.charAt(i);
if ((fullEmail>= 'a' && fullEmail<= 'z') || (fullEmail >= 0 && fullEmail<= 9) || (fullEmail == '-') || (fullEmail == '@') || (fullEmail == '.')){
if (fullEmail == '@'){
count++;
}
}else{
alert("你的邮箱存在非法字符!");
return false;
}
}
if((Email.indexOf('@')+1)<3 || (Email.indexOf('@')+1)>45){
alert("‘@'符号左边至少3为最多45位字符!");
return false;
}
//不能有两个"@"
if (count != 1){
alert("邮箱不能有两个‘@’");
return false;
}
}else{
alert("邮箱开头不能有‘@’或‘.’ 并且结尾不能有‘@’或 '.'!");
return false;
}
}else{
alert("请正确填写你常用的邮箱地址!");
return false;
}
return true;
}
</script>
</head>
<body>
<form>
<table>
<tr>
<td>请输入邮箱地址:</td>
<td>
<input type="text" id="valueEmail">
</td>
<td>
<input type="button" value=" 验证邮箱是否正确 " οnclick="emailCheck();">
</td>
</tr>
</table>
</form>
</body>
</html>