HTML页⾯登录时的JS验证⽅法
开发⼀个注册的HTML页⾯,⽤于搜集⽤户的注册信息。包括:姓名(不能为空),年龄(必须超过17岁),体重(30-150kg),班级(下拉列表),登陆密码(⾄少8位长)、确认密码(和登录密码⼀致),Email(不能为空) , 电话,QQ,个⼈简历等信息。并针对这些表的元素来创建相应的验证,如果检测到错误,在输⼊框后⾯⽤红⾊的字显⽰错误。要⽤到前⾯⼏节学习过的单⾏⽂本输⼊框text、下拉列表框select、密码输⼊框password、多⾏⽂本输⼊框textarea。这是⼀个较实⽤的⽤户注册表单.。
register.html:
复制代码代码如下:
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>实验2</title>
<link href="check.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="load.js">
</script>
</head>
<!--return validate()和validate()在于是否清空表单-->
<body onload="load_greeting()">
<form id="test" align="left" onSubmit="return validate()">
<table>
<tr>
<td>Name*:</td>
<td><input type="text" name="Name" id="name" size="20" onChange='check("name")'></td>
<td id="nameCheck" class="check" hidden="true">*姓名不能为空</td>
</tr>
<tr>
<td>Age:</td>
<td><input type="text" name="Age" id="age" size="20" onChange='check("age")'></td>
<td id="ageCheck" class="check" hidden="true">*年龄不能⼩于17岁</td>
</tr>
<tr >
<td>weight:</td>
<td><input type="text" name="weight" id="weight" size="20" onChange='check("weight")'></td>
<td id="weightCheck" class="check" hidden="true">*体重范围为30~150KG</td>
</tr>
<tr>
<td>Class:</td>
<td><select id="class" name="class">
<option>class0</option>
<option>class1</option>
<option>class2</option>
<option>class3</option>
</select>
</td>
</tr>
<tr>
<td>Password*:</td>
<td><input type="password" name="Password" id="password" size="20" onChange='check("password")'></td>
<td id="passwordCheck" class="check" hidden="true">*password length less than 8</td>
</tr>
<tr>
<td>Confirm Password*:</td>
<td><input type="password" name="cpassword" id="cpassword" size="20" onChange='check("cpassword")'></td>
html怎么实现登录验证功能<td id="cpasswordCheck" class="check" hidden="true">*Two passwd is not same</td>
</tr>
<tr >
<td>Email*:</td>
<td><input type="email" name="email" id="email" size="20" onChange='check(this.id)'></td> <td id="emailCheck" class="check" hidden="true">*电⼦邮件名⾮法!</td>
</tr>
<tr>
<td>TEL:</td>
<td><input type="text" name="TEL" id="TEL" size="20"></td>
</tr>
<tr>
<td>QQ:</td>
<td><input type="text" name="QQ" id="QQ" size="20"></td>
</tr>
<tr>
<td>Personal Information:</td>
<td><textarea rows="10" cols="19"></textarea></td>
</tr>
<tr>
<td colspan="3">
<input type="submit" name="submit">
<input type="reset" name="reset">
</td>
</tr>
</table>
</form>
</body>
</html>
check.css:
复制代码代码如下:
td.check{
color:#C00;
font-weight:bold;
}
load.js:
复制代码代码如下:
function check(str)
{
var x = ElementById(str);
var y = ElementById(str+"Check");
//alert("check!");
if(str=="name")
{
if(x.value=="")
y.hidden = false;
else
y.hidden = true;
}
else if(str=="age")
{
if(isNaN(x.value) || x.value < 17)
y.hidden = false;
else
y.hidden = true;
}
else if(str=="weight")
{
x = x.value;
if(isNaN(x) || x < 30 || x > 150)
y.hidden = false;
else
y.hidden = true;
}
else if(str=="password")
{
x = x.value.length;
if(x < 8)
{
y.hidden = false;
//alert("check!");
}
else
y.hidden = true;
}
else if(str=="cpassword")
{
var z = ElementById("password").value;
x = x.value;
if(x != z)
y.hidden = false;
else
y.hidden = true;
}
else if(str=="email")
{
x = x.value.indexOf("@")
if(x == -1)
y.hidden = false;
else
y.hidden = true;
}
return y.hidden;
}
function validate()
{
var arr=["name", "age", "weight", "password", "cpassword", "email"]; var i = 0;
submitOK = true;
while(i <= 5)
{
if(!check(arr[i]))
{
alert(arr[i]+" wrong!");
submitOK = false;
break;
}
i++;
}
if(submitOK)
{
alert("提交成功!");
return true;
}
else
{
alert("提交失败");
return false;
}
}
function load_greeting()
{
//alert("visit \n"); }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论