用户注册页面代码javascript验证.docx
《用户注册页面代码javascript验证.docx》由会员分享,可在线阅读,更多相关《用户注册页面代码javascript验证.docx(10页珍藏版)》请在冰豆网上搜索。
![用户注册页面代码javascript验证.docx](https://file1.bdocx.com/fileroot1/2023-4/20/014f6faf-1158-4637-b8c7-87a16193893d/014f6faf-1158-4637-b8c7-87a16193893d1.gif)
用户注册页面代码javascript验证
用户注册页面代码\javascript验证
<%@pagecontentType="text/html;charset=gb2312"%>
functionisValidate(form)
{
//得到用户输入的信息
userid=form.userid.value;
username=form.username.value;
userpass=form.userpass.value;
userpass2=form.userpass2.value;
birthday=form.birthday.value;
email=form.email.value;
address=form.address.value;
phone=form.phone.value;
//判断用户ID长度
if(!
minLength(userid,6))
{
alert("用户ID长度小于6位!
");
form.userid.focus();
returnfalse;
if(!
maxLength(userid,8))(
alert("用户ID长度大于8位!
)
form.userid.focus();
returnfalse;
}
//判断用户名长度
if(!
minLength(username,6))
(
alert("用户名长度小于6位!
)
form.username.focus();
returnfalse;
}
if(!
maxLength(username,8))
(
alert("用户名长度大于8位!
)
form.username.focus();
returnfalse;
}
//判断口令长度
if(!
minLength(userpass,6))
alert("口令长度小于6位!
");
form.userpass.focus();
returnfalse;
)
if(!
maxLength(userpass,8))
(
alert("口令长度大于8位!
");
form.userpass.focus();
returnfalse;
)
//判断用户名和口令是否相同
if(username==userpass)
(
alert("用户名和口令不能相等!
");
form.userpass.focus();
returnfalse;
)
〃验证两次口令是否相同
if(userpass!
=userpass2)(
");
alert("两次输入的口令不相同!
form.userpass.focus();
returnfalse;
)
〃验证生日的格式是否正确
if(!
isDate(birthday))
(
alert("生日的格式不正确!
");
form.birthday.focus();
returnfalse;
)
//验证email的格式是否正确
if(!
isEmail(email))
(
alert("Email格式不正确!
");
form.email.focus();
returnfalse;
)
〃验证电话号码的格式是否正确
if(!
isDigital(phone))
(
alert("电话号码的格式不正确");
form.phone.focus();
returnfalse;
〃验证地址的长度是否正确
if(!
maxLength(address,50))
(
alert("地址长度大于50位!
");
form.address.focus();
returnfalse;
)
returntrue;
)
//验证是否是空
functionisNull(str)
(
if(str.length==0)
returntrue;
else
returnfalse;
)
〃验证是否满足最小长度
functionminLength(str,length)
if(str.length>=length)
returntrue;
else
returnfalse;
)
//判断是否满足最大长度
functionmaxLength(str,length)
(
if(str.length<=length)
returntrue;
else
returnfalse;
)
//判断是否是数字
functionisDigital(str)
(
for(i=0;i(
//允许使用连字符
if(str.charAt(i)>='0'&&str.charAt(i)<='9'
||str.charAt(i)=="-"&&i!
=0&&i!
=str.length-1)continue;
else
returnfalse;
)
returntrue;
)
//判断是否是整数
functionisNumber(str)
(
for(i=0;i(
//如果要判断小数,需要判断小数点
if(str.charAt(i)>='0'&&str.charAt(i)<='9'
||str.charAt(i)=="-"&&i==0)continue;
else
returnfalse;
)
returntrue;
)
//判断是否是日期,日期的格式为1988-1-1
functionisDate(date)
//查找分隔符
indexl=date.indexOf("-");
//如果分隔符不存在,则不是合法的时间
if(index1==-1)
returnfalse;
//获取时间中的年
year=date.substring(0,index1);
//获取时间中的剩下部分
date=date.substring(index1+1);
//查找第二个分隔符
index1=date.indexOf("-");
//如果不存在第二个分隔符,则不是合法的时间
if(index1==-1)
returnfalse;
//获取时间中的月份
month=date.substring(0,index1);
//获取时间中的日
day=date.substring(index1+1);
//判断是否是数字,如果不是则不是合法的时间
if(isNumber(year)&&isNumber(month)&&isNumber(day))
{
//判断基本范围
if(year<1900||year>9999||month<1||month>12||day<1)
returnfalse;
//判断31天的月
if((month==1||month==3||month==5||month==7
||month==8||month==10||month==12)&&day>31)returnfalse;
//判断30天的月
if((month==4||month==6||month==9||month==11)
&&day>30)returnfalse;
//如果是2月,判断是否为润年
if(month==2)
{
if(year%400==0||(year%4==0&&year%100!
=0))
{
if(day>29)
returnfalse;
}else
{
if(day>28)
returnfalse;
else
returnfalse;
returntrue;
)
//判断是否是Email
functionisEmail(email)
(
if(email.length==0)
returnfalse;
index1=email.indexOf('@');
index2=email.indexOf('.');
if(index1<1//@符号不存在,或者在第一个位置
||index2<1//.符号不存在,或者在第一个位置
||index2-index1<2//.在@的左边或者相邻
||index2+1==email.length)//.符号后面没有东西
returnfalse
else
returntrue;
)
注册界面
请注册
isValidate(form1)">
用户ID: | 6—8位<例><">用户名: | | 口令: | | 确认口令: | | 生日: | 格式为: 1988-1-1 | 学历: | 专科 本科 硕士研究生 博士研究生 其他 | 地区: | 华东 华南 华北 东d匕 东南 西南 西d匕 东d匕 华中 | E-mail: | | 地址: | | 电话: | | 备注: |
| |
| |