简单的数据校验
校验用户名,长度不能小于6位
1.确定事件:提交事件onsubmit
2.事件要触发函数checkForm()
3.函数中要去做一些校验
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<script>
function checkForm(){
//用户名校验
var input1 = document.getElementById("username");
var uValue = input1.value;
if(uValue.length <6){
alert("用户名至少6位");
return false;
}
//邮箱校验
var email = document.getElementById("email")
var uEmail = email.value;
if(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(uEmail)){
alert("校验成功");
}else{
alert("校验失败")
return false;
}
return true;;
}
</script>
</head>
<body>
<form action="../01-网站首页的优化/网站首页.html" onsubmit="return checkForm()">
用户名:<input type="text" id="username"/><br />
密码:<input type="password" id="password" /><br />
邮箱:<input type="text" id="email" /><br /> <!--如果采用type="email"则会自动校验-->
<input type="submit" value="提交" />
</form>
</body>
</html>