<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<script>
function check() {
var firstname = document.getElementById('firstname');
var lastname = document.getElementById('lastname');
var email = document.getElementById('email');
var password = document.getElementById('password');
var chek_password = document.getElementById(' chek_password');
//教研名字
var firstnamev = firstname.value;
if (firstnamev == '') {
alert('名字不能为空');
return false;
} else {
for (var i = 0; i < firstnamev.length; i++) {
var c = firstnamev[i];
if (c >= 0) {
alert('名字不能包含数字');
return false;
}
}
}
//教研姓氏
var lastnamev = lastname.value;
if (lastnamev == '') {
alert('姓氏不能为空');
return false;
} else {
for (var j = 0; j < lastnamev.length; j++) {
var cl = lastnamev[j];
if (cl >= 0) {
alert('姓氏不能包含数字');
return false;
}
}
}
//校验邮箱
var emailv = email.value;
if (emailv == '') {
alert('邮箱不能为空!');
return false;
} else {
var index1 = emailv.indexOf('@');//字符串中包含“@”
var index2 = emailv.indexOf('.');//字符串中包含“.”
if (index1 == -1 || index2 == -1) {//@,.不存在
alert('邮箱地址不合法!');
return false;
} else if (index1 >= index2) {//@在.的后面,索引大于.
alert('邮箱地址不合法!');
return false;
}
}
//校验密码
var p = password.value;
if (p == '') {
alert('密码不能为空!');
return false
} else if (p.length <= 6) {
alert('密码长度小于6!');
return false;
}
//再次校验密码 ????不能实现
if (chek_password.value = '') {
alert('请再次输入密码');
return false;
}
else if (password.value != chek_password.value) {//这里不能确定
alert('两次输入密码不一致');
return false;
}
}
</script>
</head>
<body>
<form action="do.html" onsubmit="return check();">
名字:<input type="text" id="firstname"><br>
姓氏:<input type="text" id="lastname"><br>
邮箱:<input type="text" name="" id="email"><br>
密码:<input type="password" name="" id="password"><br>
再次输入密码:<input type="password" name="" id="check_password"><br>
<input type="submit" value="提交">
</form>
</body>
</html>
var chek_password = document.getElementById(' chek_password');
这段代码字母拼写错误了...应该是check_password
我在检查一下。初学js。
小学生666,加油
0.0