layui表单验证内置⾃定义规则-使⽤说明
关于 layui表单的验证规则、调⽤、⾃定义规则的使⽤经验总结:
除了 layui 本⾝配套的⼀个验证体系,还⽀持开发者⾃定义验证规则(如上表中的【⾃定义密码验证(代码参考下⽂)】),并直接嵌⼊到页⾯的js代码中。
必要属性:lay-verify="required|pass",如果多个规则验证,就⽤符号|并列即可。
截图举例:
验证表单对象 · 都有哪些?
如何调⽤规则和验证? 如下表所⽰:
验证表单对象 / 元素部分重要验证规则的设定代码调⽤实例(参考下⽂的代码实例)
其他
注解
说明
验证必填项lay-verify="required"<input type="text" name="" lay-verify="required" placeholder="请输⼊" autocomplete="off"
class="layui-input">
省略
多规则验证
lay-
verify="required|number"
<input type="text" name="number" lay-verify="required|number" autocomplete="off" class="layui-
input">
省略input标签placeholder属性
验证⽤户名
lay-
verify="required|username"
<input type="text" name="username" lay-verify="required|username" placeholder="请输⼊⽤户
名" autocomplete="off" class="layui-input">
省略
验证⼿机号
lay-
verify="required|phone"
<input type="tel" name="phone" lay-verify="required|phone" autocomplete="off" class="layui-
input">
省略
验证邮箱lay-verify="required|email"<input type="text" name="email" lay-verify="required|email" autocomplete="off" class="layui-
input">
省略
验证⾝份证
lay-
verify="required|identity"<input type="text" name="identity" lay-verify="required|identity" placeholder=""
autocomplete="off" class="layui-input">
省略
验证⽇期lay-verify="required|date"<input type="text" name="date" id="LAY-component-form-group-date" lay-verify="required|date"
placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input" lay-key="1">
省略
验证链接lay-verify="required|url"<input type="tel" name="url" lay-verify="required|url" autocomplete="off" class="layui-input">省略
⾃定义密码
验证lay-verify="required|pass"
<input type="password" name="password" lay-verify="required|pass" placeholder="请输⼊密码"
autocomplete="off" class="layui-input">
省略
验证select 选择框lay-verify="required" lay-
search=""
见下⽂代码省略
验证单选框中⽂中⽂后续改动
验证复选框中⽂中⽂后续改动
验证表单对象 / 元素部分重要验证规则的设定代码调⽤实例(参考下⽂的代码实例)
其他
注解
说明
具体实例代码参考:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>表单验证 - 案例演⽰</title>
<meta name="keywords" content="" />
<meta name="description" content="" />
<meta name="renderer" content="webkit" />
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="format-detection" content="telephone=no" />
<link rel="stylesheet" href="plugins/layui/css/layui.css">
<link rel="stylesheet" href="css/global.css">
<!-- 让IE8/9⽀持媒体查询,从⽽兼容栅格 -->
<!--[if lt IE 9]>
<script src="/html5shiv/r29/html5.min.js"></script>
<script src="/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
</head>
<body>
<div class="main-content">
<form class="layui-form" action="" lay-filter="component-form-group">
<div class="layui-form-item">
<label class="layui-form-label">验证必填项</label>
<div class="layui-input-block">
<input type="text" name="" lay-verify="required" placeholder="请输⼊" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">多规则验证</label>
<div class="layui-input-block">
<input type="text" name="number" lay-verify="required|number" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">验证⽤户名</label>
<div class="layui-input-block">
<input type="text" name="username" lay-verify="required|username" placeholder="请输⼊⽤户名" autocomplete="off" class="layui-input"> </div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">验证⼿机号</label>
<div class="layui-input-inline">
<input type="tel" name="phone" lay-verify="required|phone" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<div class="layui-inline">
<label class="layui-form-label">验证邮箱</label>
<div class="layui-input-inline">
<input type="text" name="email" lay-verify="required|email" autocomplete="off" class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">验证⾝份证</label>
<div class="layui-input-inline">
<input type="text" name="identity" lay-verify="required|identity" placeholder="15或18位" autocomplete="off" class="layui-input">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">验证⽇期</label>
<div class="layui-input-inline">
<input type="text" name="date" id="LAY-component-form-group-date" lay-verify="required|date" placeholder="yyyy-MM-dd" autocomplete="off" class ="layui-input" lay-key="1">
</div>
</div>
<div class="layui-inline">
<label class="layui-form-label">验证链接</label>
<div class="layui-input-inline">
<input type="tel" name="url" lay-verify="required|url" placeholder="" autocomplete="off" class="layui-input">
</div>
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">⾃定义密码验证</label>
<div class="layui-input-inline">
<input type="password" name="password" lay-verify="required|pass" placeholder="请输⼊密码" autocomplete="off" class="layui-input">
</div>
<div class="layui-form-mid layui-word-aux">请填写6到12位密码</div>
</div>
<div class="layui-form-item">
<div class="layui-inline">
<label class="layui-form-label">搜索选择框</label>
<div class="layui-input-inline">
<select name="modules" lay-verify="required" lay-search="">
<option value="">直接选择或搜索选择</option>
<option value="1">layer</option>
<option value="2">form</option>
<option value="3">layim</option>
<!-- ···略···略···略··· -->
<option value="19">switch</option>
<option value="20">radio</option>
</select>
</div>
</div>
</div>
<div class="layui-form-item">
<div class="layui-input-block">
<div class="layui-footer">
<button class="layui-btn" lay-submit="" lay-filter="component-form-demo1">⽴即提交</button>
<button type="reset" class="layui-btn layui-btn-primary">重置</button>
</div>
</div>
</div>
</form>
</div>
<!--重要⽂件-->
<!--重要⽂件-->
<script type="text/javascript" src="plugins/layui/layui.js"></script>
<!--[if lt IE 9]>
<script src="js/jquery-1.9.1.min.js"></script>
<![endif]-->
<script src="js/jquery.min.js"></script>
<script type="text/javascript">
layui.use(['layer', 'form', 'element'], function() {
var layer = layui.layer,
element = layui.element,//注意:选项卡依赖 element 模块,否则⽆法进⾏功能性操作
$ = layui.jquery,
form = layui.form;
//…
form.verify({
username: function(value, item){ //value:表单的值、item:表单的DOM对象
if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
return '⽤户名不能有特殊字符';
}
if(/(^\_)|(\__)|(\_+$)/.test(value)){
return '⽤户名⾸尾不能出现下划线\'_\'';
}
if(/^\d+\d+\d$/.test(value)){
return '⽤户名不能全为数字';
}
}
//我们既⽀持上述函数式的⽅式,也⽀持下述数组的形式
//数组的两个值分别代表:[正则匹配、匹配不符时的提⽰⽂字]
,pass: [/^[\S]{6,12}$/,'密码必须6到12位,且不能出现空格']
});
/
/表单提交按钮
/*$("#save").click(function(){
var index = FrameIndex(window.name); //获取窗⼝索引
parent.layer.close(index); // 关闭layer
window.load(); //刷新⽗页⾯
});*/
});
</script>
</body>
</html>
具体操作后的效果图省略,本⽂不赘述。
以上就是关于“ layui表单验证 && 内置⾃定义规则 - 使⽤说明 ” 的全部⽂章。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论