(贰)JS页面校验

1.用户名长度校验

  • index.jsp如下:
<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script>
		function checkForm(){
			var input1=document.getElementById("username");
			var uValue=input1.value;
			if (uValue.length>=6){
				return true;
			}
			else {
				alert("用户名太短了!!");
			}	
				return false;
		}
	</script>
</head>
<body>
	<form action="success.jsp" onsubmit="return checkForm()">
			用户名:<input type="text" id="username"/><br>
			<input type="submit" value="提交"/>
	</form>
</body>
</html>

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
	<h1>成功跳转!!!!!</h1>
</body>
</html>
  • 范围效果:
    在这里插入图片描述
    填入:12345,点击提交:
    在这里插入图片描述
  • 填入123456,点击提交:
    在这里插入图片描述

2.邮箱校验(正则表达式)

同样的写index.jsp和success.jsp两个文件,success.jsp不变,index.jsp如下:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
	<script>
		function checkForm(){
			var input1=document.getElementById("username");
			var uValue=input1.value;
			if (uValue.length>=6){
				
			}
			else {
				alert("用户名太短了!!");
				return false;
			}
			var email= document.getElementById("email");
			var uEmail= email.value;
			if(/^[a-z0-9]+([._\\-]*[a-z0-9])*@([a-z0-9]+[-a-z0-9]*[a-z0-9]+.){1,63}[a-z0-9]+$/.test(uEmail)){
				alert("校验成功!");
			}
			else{
				alert("邮箱不合法!!");
				return false;
			}
			return true;
		}
	</script>
</head>
<body>
	<form action="success.jsp" onsubmit="return checkForm()">
			用户名:<input type="text" id="username"/><br>
			密码:<input type="password" id="password"/><br>
			邮箱:<input type="text" id="email"/><br>
			<input type="submit" value="提交"/>
	</form>
</body>
</html>
  • 访问效果如下:
  • 在这里插入图片描述
  • 用户名输入:123456,密码随便。邮箱输入:123(不合规则),则:
  • 在这里插入图片描述
  • 输入正确的邮箱:1234@qq.com.cn
  • 在这里插入图片描述
  • 校验成功,进而跳转到成功页面。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值