分享学做的一个jsp注册页面
分享一个自己学习时,用bootstrap,多方搜索做的注册页面,包括页面的非空验证。导入相关的bootstrap的js和css文件就可以了。背景很丑,可以自己换一个。后面进一步完善<( ̄︶ ̄)↗[GO!]
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>bootstrap注册页面</title>
<link rel="stylesheet" type="text/css" href="css/bootstrap.min.css" />
<link rel="stylesheet" type="text/css" href="css/bootstrap-theme.min.css" />
<script src="js/jquery-1.12.0.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/bootstrap.min.js" type="text/javascript" charset="utf-8"></script>
<script src="js/jquery.validate.min.js" type="text/javascript"></script>
<script>
//自定义校验规则
$.validator.addMethod(
// 规则的名称
"checkUsername",
// 校验的函数
function(value, element, params) {
// 参数value代表输入的内容,element代表被校验的元素对象,params代表规则对应的参数值
// 目的:对输入的username进行ajax异步校验
// 定义一个标志
var flag = true;
$.ajax({
"async" : false,// 是否异步
"url" : "${pageContext.request.contextPath}/checkUsername",// 提交地址
"data" : {
"username" : value
},// 传输的数据
"type" : "POST",// 提交方式
"dataType" : "json",// 返回的数据类型
"success" : function(data) {// 成功后的回调函数
flag = data.isExist;
alert(flag);
}
});
// 返回false代表该校验器不通过
return !flag;
});
$(function() {
$("#registForm").validate({
rules : {
"username" : {
"required" : true,
"checkUsername" : true
},
"password" : {
"required" : true,
"rangelength" : [ 6, 12 ]
},
"repassword" : {
"required" : true,
"rangelength" : [ 6, 12 ],
"equalTo" : "#password"
},
"email" : {
"required" : true,
"email" : true,
},
"sex" : {
"required" : true
}
},
messages : {
"username" : {
"required" : "用户名不能为空",
"checkUsername" : "该用户名已存在"
},
"password" : {
"required" : "密码不能为空",
"rangelength" : "密码长度为6-12位"
},
"repassword" : {
"required" : "确认密码不能为空",
"rangelength" : "密码长度为6-12位",
"equalTo" : "两次输入的密码不一致"
},
"email" : {
"required" : "邮箱不能为空",
"email" : "邮箱格式不正确",
}
}
});
});
</script>
<style type="text/css">
body{
/*background-image: url(img/bg8.jpg);*/
/*background-repeat: no-repeat;*/
/*background-color: #191970;*/
background: radial-gradient(ellipse closest-side,
#00CC66, #006666 20%, #0066CC 50%, #0033CC);
}
label{
text-align: justify;
text-align-last: justify;
line-height: 35px;
}
.error {
color: red;
}
</style>
</head>
<body>
<div class="container">
<div class="col-md-4"></div>
<div class="col-md-6 row" style="margin-top: 50px;background-color: whitesmoke;border-radius: 5px;">
<div style="margin-top: 30px;margin-bottom: 30px;margin-right: 20px;">
<form id="registForm" action="${pageContext.request.contextPath}/register" method="post">
<div class="form-group row">
<label for="username" class="col-sm-2 col-form-label">用户名</label>
<div class="col-sm-10"><input type="text" class="form-control" id="username" name="username" placeholder="请输入账号"> </div>
</div>
<div class="form-group row">
<label for="password" class="col-sm-2 col-form-label">密码</label>
<div class="col-sm-10"><input type="password" class="form-control" id="password" name="password" placeholder="请输入密码"></div>
</div>
<div class="form-group row">
<label for="password" class="col-sm-2 col-form-label">确认密码</label>
<div class="col-sm-10"><input type="password" class="form-control" id="repassword" name="repassword" placeholder="请确认密码"></div>
</div>
<div class="form-group row">
<label for="password" class="col-sm-2 col-form-label">邮箱</label>
<div class="col-sm-10"><input type="email" class="form-control" name="email" placeholder="请输入邮箱"></div>
</div>
<div class="form-group row">
<label for="password" class="col-sm-2 col-form-label">姓名</label>
<div class="col-sm-10"><input type="text" class="form-control" name="user" placeholder="请输入姓名"></div>
</div>
<div class="form-group row">
<label for="sex" class="col-sm-2 col-form-label">性别</label>
<div class="col-sm-10">
<div class="form-check form-check-inline" style="line-height: 35px;">
<input class="form-check-input" type="radio" name="sex" id="sex1" value="male" checked>男
<input class="form-check-input" type="radio" name="sex" id="sex2" value="female">女
<label class="error" for="sex" style="display: none;">性别必须勾选</label>
</div>
</div>
</div>
<div class="form-group row">
<label for="password" class="col-sm-2 col-form-label">验证码</label>
<div class="col-sm-8"><input type="text" class="form-control" id="repassword" placeholder="请输入验证码"></div>
<div class="col-sm-2"><img src="img/bg3.jpg" height="30px"/></div>
</div>
<div class="form-group row">
<div class="col-sm-2"></div>
<!--给按钮加上btn-lg的class就是大号的按钮-->
<div class="col-sm-10"><input type="submit" value="我要注册" class="btn btn-primary btn-block" /></div>
</div>
<div class="form-group row">
<div class="col-sm-2"></div>
<div class="col-sm-5" style="float: left;">已经有账号了?</div>
<div class="col-sm-5"><a href="login.html" style="float: right;">直接登录</a></div>
</div>
</form>
</div>
</div>
<div class="col-md-2"></div>
</div>
</body>
</html>
效果如下: