<!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=utf-8" />
<title>无标题文档</title>
<style type="text/css">
.ok{color:green;}
.err{color:red;}
</style>
<script src="haha.js"></script>
</head>
<body>
<form action="t1.html" method="post" onsubmit="return chk1()">
邮箱:<input id="mail" onblur="chk1();" placeholder="请输入邮箱"/><span id="msg1"></span>
<br/>
<input type="submit"/>
</form>
<script>
function chk1(){
var txt=getEl("mail");
var msg1=getEl("msg1");
var val=txt.value;
if(val.length==0){
msg1.innerHTML="邮箱不能为空!";
msg1.className="err";
return false;
}
if(chkMail(val)){
msg1.innerHTML="邮箱格式正确!";
msg1.className="ok";
return true;
}else{
msg1.innerHTML="邮箱格式错误!";
msg1.className="err";
return false;
}
}
</script>
</body>
</html>
// JavaScript Document
function getEl(id){
return document.getElementById(id);
}
function getEls(obj,tag){
return obj.getElementsByTagName(tag);
}
function chkMail(mail){
var n=mail.lastIndexOf(".");
var m=mail.indexOf("@");
var len=mail.length;
if(m>0 && n>m+1 && n<len-1){
return true;
}
return false;
}