作业

2019.01.14作业

实现登录表单的验证功能

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<script>
			function checkForm(){
			if(checkUsername() && checkPassword()){
				return true;
			}
				return false;
			}
			function checkUsername(){
				var username = document.forms[0].username.value;
				if(username.length==0){
					alert("请输入用户名");
					return false;
				}
				for(i=0;i<username.length;i++){
					var c = username.charAt(i);
					if(!(c>='0' && c<='9' || c>='a' && c<='z' || c == "_")){
						alert("会员名包含非法字符,只能包含a-z、0-9 和下划线");
						return false;
					}
				}
				return true;
			}
			
			function checkPassword(){
				var password = document.forms[0].password.value;
				if(password.length == 0){
					alert("未输入密码请输入密码");
					return false;
				}
				if(password.length < 6){
					alert("密码位数不能少于6位,请重新输入");
					return false;
				}
				return true;
			}
		</script>
	</head>
	<body>
		<form onsubmit="return checkForm()">
			<table>
				<tr>
					<td>会员名:</td>
					<td>
						<input type="text" name="username">
					</td>
					<td>(可包含 a-z、0-9 和下划线)</td>
				</tr>
				<tr>
					<td>密码:</td>
					<td>
						<input type="password" name="password">
					</td>
					<td>(至少包含 6 个字符)</td>
				</tr>
				<tr>
					<td colspan="2" align="center">
						<input type="submit" value="登录">
						<input type="reset" value="重填">
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

实现注册表单的验证功能

<!DOCTYPE html>
<html>
	<head>
		<meta charset="UTF-8">
		<title></title>
		<style>
			.c1{
				background-color: #E7FBFF;
				border: 1px solid blue;
			}
		</style>
		<script>
			function a(){
				if(b()&&c()&&d()&&e()&&f()){
					return false;
				}
				return true;
			}		
			function b(){
				var username = document.forms[0].username.value;
				if(username.length==0){
					alert("登录名不能为空");
					return false;
				}
				return true;
			}
			function c(){
				var password1 = document.forms[0].password1.value;
				var password2 = document.forms[0].password2.value;
				if(password1.length==0){
					alert("密码不能为空");
					return false;
				}
				if(password1.length<6){
					alert("密码至少包含 6 个字符")
					return false;
				}
				if(password2.length==0){
					alert("再次输入密码不能为空");
					return false;
				}
				if(password1.substring(0) != password2.substring(0)){
					alert("密码和再次输入密码必须相同");
					return false;
				}
				return true;
			}			
			function d(){
				var year = document.forms[0].year.value;
				if(year.length==0){
					alert("年份不能为空");
					return false;
				}
				var y = year.substring(0,2);
				if(y != "20"){
					alert("年份必须是20开头")
					return false;
				}
				return true;
			}			
			function e(){
				var month = document.forms[0].month.value;
				if(month == "[选择月份]"){
					alert("月份不能为空");
					return false;
				}
				return true;
			}	
			function f(){
				var date = document.forms[0].date.value;
				if(date.length==0){
					alert("日期不能为空");
					return false;
				}
				if(1 <= parseInt(date.substring()) <=31){
					alert("日期必须在1到31之间")
					return false;
				}
				return true;
			}
		</script>
	</head>
	<body>
		<form onsubmit="return a()">
			<table>
				<tr align="center">
					<td class="c1">登录名:</td>
					<td>
						<input type="text" name="username" >
					</td>
				</tr>
				<tr align="center">
					<td class="c1">密码:</td>
					<td>
						<input type="password" name="password1">
					</td>
					<td>(至少包含 6 个字符)</td>
				</tr>
				<tr align="center">
					<td class="c1">再次输入密码:</td>
					<td>
						<input type="password" name="password2">
					</td>
				</tr>
				<tr>
					<td class="c1">出生日期:</td>
					<td colspan="2">
						<input type="text" size="4" name="year" placeholder="yyyy">年&nbsp;&nbsp;
						<select name="month">
							<option>[选择月份]</option>
							<option>1</option>
							<option>2</option>
							<option>3</option>
							<option>4</option>
							<option>5</option>
							<option>6</option>
							<option>7</option>
							<option>8</option>
							<option>9</option>
							<option>10</option>
							<option>11</option>
							<option>12</option>
						</select>月&nbsp;&nbsp;
						<input type="text" size="1" name="date" placeholder="dd">日
					</td>
				</tr>
				<tr>
					<td class="c1">
						<input type="reset" value="重填">
					</td>
					<td colspan="2">
						<input type="submit" value="同意以下注册条款,提交注册信息">
					</td>
				</tr>
			</table>
		</form>
	</body>
</html>

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值