<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>姓名和邮箱验证</title>
<script src="js/jquery-3.2.1.min.js" type="text/javascript" charset="utf-8"></script>
</head>
<body>
<form action="/new_file.html" method="post" onsubmit="return check()">
姓名:<input type="text" id="name" class="name"/><span id="nametip"></span><br>
邮箱:<input type="text" id="email" class="email" /><span id="emailtip"></span><br>
<input type="submit" id="submit" value="提交" />
</form>
<script type="text/javascript">
$("#name").blur(function(){
var name = $("#name").val();
if (name.length<3) {
$("#nametip").html("用户名格式不正确");
$("#nametip").css({"color":"red"});
} else{
$("#nametip").html("√");
}
});
$("#email").blur(function(){
var em = $("#email").val();
if (em.indexOf("@")!=-1) {
$("#emailtip").html("√");
} else{
$("#emailtip").html("邮箱名格式不正确");
$("#emailtip").css({"color":"red"});
}
});
function check(){
var name = $("#name").val();
if (name.length<3) {
return false;
} else{
return true;
}
}
</script>
</body>
</html>