文档地址
首先引入
代码语言:javascript复制<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>
以laravel 模板为例
代码语言:javascript复制<form action="{{route('admin.user.add')}}" method="post" class="layui-form" id="form-user-add">
@csrf
<div class="layui-form-item">
<label class="layui-form-label">
<span class="x-red">*</span>姓名</label>
<div class="layui-input-inline">
<input type="text" name="tusername" class="layui-input" value="{{old('tusername')}}"
{{-- autocomplete="off"--}}
></div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">
<span class="x-red">*</span>账号</label>
<div class="layui-input-inline">
<input type="text" name="username" class="layui-input" value="{{old('username')}}"
{{-- autocomplete="off"--}}
></div>
<div class="layui-form-mid layui-word-aux">
<span class="x-red">*</span>将会成为您唯一的登入名
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"><span class="x-red">*</span>性别</label>
<div class="layui-input-block">
<input type="radio" name="sex" value="先生" lay-skin="primary" title="先生" checked="">
<input type="radio" name="sex" value="女士" lay-skin="primary" title="女士">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">
<span class="x-red">*</span>邮箱</label>
<div class="layui-input-inline">
<input type="email"name="email" class="layui-input" value="{{old('email')}}">
</div>
<div class="layui-form-mid layui-word-aux">
<span class="x-red">*</span>忘记密码时使用邮箱重置密码
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">
<span class="x-red">*</span>手机号</label>
<div class="layui-input-inline">
<input type="text" name="phone" class="layui-input" value="{{old('phone')}}">
</div>
</div>
<div class="layui-form-item">
<label class="layui-form-label">
<span class="x-red">*</span>密码</label>
<div class="layui-input-inline">
<input type="password" name="password" class="layui-input" id="password"></div>
<div class="layui-form-mid layui-word-aux">6到16个字符</div></div>
<div class="layui-form-item">
<label class="layui-form-label">
<span class="x-red">*</span>确认密码</label>
<div class="layui-input-inline">
<input type="password" name="password_confirmation" class="layui-input"></div>
</div>
<div class="layui-form-item">
<label class="layui-form-label"></label>
<button class="layui-btn">增加用户</button></div>
</form>
代码语言:javascript复制 $('#form-user-add').validate({
rules: {
tusername: {
required: true
},
username:{
required: true,
},
phone:{
phone:true
},
password: {
required: true,
rangelength:[5,10]
},
password_confirmation:{
equalTo:"#password"
}
},
messages: {
tusername: {
required: "真实姓名不能为空",
},
username: {
required: "用户名不能为空"
},
password: {
required: "密码不能为空",
rangelength:"6到16个字符"
},
confirmation_password:{
equalTo:"两次输入密码不一致"
}
},
onKeyup:false,
success:"valid",
//验证通过后处理
submitHandler:function (form) {
form.submit();
}
});
// 自定义验证手机号
jQuery.validator.addMethod("phone", function(value, element) {
var reg0 = /^ 86-1[3-9]d{9}$/;
var reg1 = /^1[3-9]d{9}$/;
var reg = reg0.test(value) || reg1.test(value);
return this.optional(element) || reg;
}, "请正确填写您的手机号");