index2.html function checkForm(){if(checkUserName()checkPass()checkBirth()checkEmail()){return true;}else{return false;}}//用户名非空+长度+合法性验证function checkUserName(){var name = document.myform.txtUser;if(name.value==""){
function checkForm(){
if(checkUserName()&&checkPass()&&checkBirth()&&checkEmail()){
return true;
}else{
return false;
}
}
//用户名非空+长度+合法性验证
function checkUserName(){
var name = document.myform.txtUser;
if(name.value==""){
alert("请输入用户名");
name.focus();
return false;
}
if(name.value.length<4||name.value.length>16){
alert("用户名输入的长度4-16个字符");
name.select();
return false;
}
for(var i=0;i
='0' && charTest<='9')) && (!(charTest>='a' &&charTest<='z')) && (charTest!='_') )
{
alert("用户名包含非法字符,只能包括字母,数字和下划线");
name.select();
return false;
}
}
return true;
}
//密码非空+长度+密码确认验证
function checkPass(){
var pass=document.myform.txtPass;
var rpass=document.myform.txtRPass;
if(pass.value==""){
alert("密码不能为空");
pass.focus();
return false;
}if(pass.value.length<6||pass.value.length>16){
alert("密码长度为6-16个字符");
pass.select();
return false;
}
if(rpass.value!=pass.value){
alert("确认密码与密码输入不一致");
rpass.select();
return false;
}
return true;
}
//出生日期验证
function checkBirth(){
var birth=document.myform.txtBirth.value;
if(birth==""){
alert("请填写出生日期");
return false;
}else if(birth.length<10){
alert("出生日期格式错误");
return false;
}else{
//截取字符串分别获得年月日
var year=birth.substring(0,4);
var month=birth.substring(5,7);
var day=birth.substring(8,birth.length);
if(birth.charAt(4)!="-"||birth.charAt(7)!="-"){//判断日期是否符合格式要求
alert("出生日期格式yyyy-mm-dd");
document.myform.txtBirth.select();
return false;
}else if(isNaN(year)||isNaN(month)||isNaN(day)){
alert("年月日必须是数字");
document.myform.txtBirth.select();
return false;
}
var time=new Date();
if(parseInt(year,10)<1900||parseInt(year,10)>time.getYear()){
alert("出生日期范围从1900年-"+time.getYear()+"年");
document.myform.txtBirth.select();
return false;
}else if(parseInt(month,10)<1||parseInt(month,10)>12){
alert("您输入的月份不在1-12月之间");
document.myform.txtBirth.select();
return false;
}else if(parseInt(day,10)<1||parseInt(day,10)>31){
alert("您输入的天数不在1-31之间");
document.myform.txtBirth.select();
return false;
}
}
return true;
}
//电子邮件验证
function checkEmail(){
var strEmail=document.myform.txtEmail;
if (strEmail.value.length==0)
{
alert("电子邮件不能为空!");
strEmail.focus();
return false;
}
if (strEmail.value.indexOf("@",0)==-1)
{
alert("电子邮件格式不正确\n必须包含@符号!");
strEmail.select();
return false;
}
if (strEmail.value.indexOf(".",0)==-1)
{
alert("电子邮件格式不正确\n必须包含.符号!");
strEmail.select();
return false;
}
return false;
}
