一.jQuery Validate验证框架
1.引入相关插件路径
在https://www.runoob.com/jquery/jquery-plugin-validate.html 里引入相关插件路径
<!--引入jQuery库-->
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
<!--前端验证插件-->
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
<!--默认的验证错误信息显示内容(中文)-->
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
2. 修改一些规则
<script>
$(function (){
$("#myform01").validate({
rules: {
account: {
required: true,
minlength: 5,
maxlength: 10,
},
},
messages:{
account: {
required: "账号不能为空",
minlength: "你输入的字符小于5个",
maxlength: "你输入的字符大于10个",
},
},
onfocusout:false,
onkeyup:false
});
})
</script>
3. 自定义验证规则
jQuery.validator.addMethod("validAccount", function(value, element) {
let reg = /^[A-Za-z]\w+$/;
return this.optional(element) || (reg.test(value));
}, "账号必须以字母开头");
4.异步验证
servlet:
package com.yu.web;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
@WebServlet("/check/account")
public class AccountCheckServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String account = request.getParameter("username");
List<String> list = new ArrayList<>();
list.add("admin123");
list.add("super123");
list.add("love123");
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
if (list.contains(account)){
out.print(false);
}else{
out.print(true);
}
out.close();
}
}
remote:{
url:"check/account",
type:"get",
dataType:"json",
data:{
username:function (){
return $.trim(($("#myAccount").val()));
},
},
}
整体代码
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>插件验证</title>
</head>
<body>
<form action="http://www.baidu.com" id="myform01">
账号:<input type="text" id="myAccount" name="account" required autofocus>
手机:<input type="text" id="myPhone" name="phone" required autofocus>
<input type="submit" value="自动提交">
</form>
<!--引入jQuery库-->
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/lib/jquery.js"></script>
<!--前端验证插件-->
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/jquery.validate.min.js"></script>
<!--默认的验证错误信息显示内容(中文)-->
<script src="http://static.runoob.com/assets/jquery-validation-1.14.0/dist/localization/messages_zh.js"></script>
<script>
$(function (){
jQuery.validator.addMethod("validAccount", function(value, element) {
let reg = /^[A-Za-z]\w+$/;
return this.optional(element) || (reg.test(value));
}, "账号必须以字母开头");
$("#myform01").validate({
rules: {
account: {
required: true,
minlength: 5,
maxlength: 10,
validAccount:true,
remote:{
url:"check/account",
type:"get",
dataType:"json",
data:{
username:function (){
return $.trim(($("#myAccount").val()));
},
},
}
},
},
messages:{
account: {
required: "账号不能为空",
minlength: "你输入的字符小于5个",
maxlength: "你输入的字符大于10个",
remote:"该账号已经被注册"
},
},
onfocusout:false,
onkeyup:false,
submitHandler:function(form){
alert("阻塞作用:可以在提交之前还能做一些事情");
return true;
}
});
})
</script>
</body>
</html>
二.layer弹出层
1.引入相关插件路径
学习网址:https://layer.layui.com/
<script src="resources/jquery-1.12.4.min.js"></script>
<script src="resources/layer/layer.js"></script>
2.警告窗口
$("#btn1").click(function () {
layer.alert("一叶<strong>遮天</strong>", {
title: "我是一个小小的警告窗口",
area: ["500px", "300px"],
icon: 3,
shade: 0.5,
closeBtn: 0,
btnAlign: "c",
time: 3000,
})
});
3.询问窗口
$("#btn2").click(function () {
layer.confirm("目光<strong style=\"color:pink;\">灼灼</strong>", {
title: "我是一个淡淡的询问窗口",
btn: ['只因你', '风华绝代', '你好美'],
area: ["500px", "300px"],
icon: 3,
shade: 0.5,
closeBtn: 0,
btnAlign: "c",
}, function (index) {
console.log(this, index);
layer.close(index);
});
});
提示窗口
$("#btn3").click(function () {
layer.msg("目光<strong style=\"color:pink;\">灼灼</strong>");
});
4.iframe窗口(加载页面)(重要)
layer提供了5种层类型。可传入的值有:0(信息框,默认)1(页面层)2(iframe层)3(加载层)4(tips层)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<a href="javascript:;" id="btn1">警告窗口</a><br/>
<a href="javascript:;" id="btn2">询问窗口</a><br/>
<a href="javascript:;" id="btn3">提示窗口</a><br/>
<a href="javascript:;" id="btn4">iframe窗口-加载页面(重要)</a><br/>
<script src="resources/jquery-1.12.4.min.js"></script>
<script src="resources/layer/layer.js"></script>
<script>
$(function (){
$("#btn1").click(function (){
layer.alert("一叶<strong>遮天</strong>",{
title : "我是一个小小的警告窗口",
area : ["500px","300px"],
icon :3,
shade : 0.5,
closeBtn : 0,
btnAlign : "c",
time : 3000,
})
});
$("#btn2").click(function (){
layer.confirm("目光<strong style=\"color:pink;\">灼灼</strong>",{
title : "我是一个淡淡的询问窗口",
btn : ['只因你','风华绝代','你好美'],
area : ["500px","300px"],
icon :3,
shade : 0.5,
closeBtn : 0,
btnAlign : "c",
},function (index){
console.log(this,index);
layer.close(index);
});
});
$("#btn3").click(function (){
layer.msg()("目光<strong style=\"color:pink;\">灼灼</strong>");
});
$("#btn4").click(function (){
layer.open({
title : "页面加载",
type : 2,
area : ['600px','500px'],
maxmain : true,
content : "check/account",
end: function(index, layero){
if(confirm('确定要关闭么')){
layer.close(index)
}
return false;
}
});
});
});
</script>
</body>
</html>
|