js验证电子邮箱

来源:互联网 发布:迅雷极速版 mac 编辑:程序博客网 时间:2024/05/01 12:05
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>js验证电子邮箱</title>
<script type="text/javascript">
function check(){
var em=document.getElementById("email");

//非空判断
if(em.value==""||em.length==0){
alert("电子邮件不能为空!");
em.focus();
return false;
}

//读取@和.的索引下标
var at=em.value.indexOf("@"); //-1
var dot=em.value.indexOf(".");

if(at<0||at==0||at==em.value.length-1){
alert("@符号不存在 或者 出现在了首位 或 结尾 都是无效。");
em.focus();
return false;
}

if(dot<0||dot==0||dot==em.value.length-1){
alert(".符号不存在 或者 出现在了首位 或结尾 都是无效");
em.focus();
return false;
}

//sdf@.com
if(at>=dot-1){
alert("@符号必须出现在.的前面,且两者之间必须有内容");
em.focus();
return false;
}

return true;
}
</script>
</head>


<body>
<form onsubmit="return check()">
请输入电子邮箱:<input type="text" id="email"/>
<input type="submit" value="提交"/>
</form>
</body>
</html>
0 0
原创粉丝点击