在使用 Bootstrap 作为前端框架的时候,表单验证时必不可少要用到 BootstrapValidator,本文介绍 BootstrapValidator 的基本使用。
Bootstrap 下载地址:https://v3.bootcss.com/getting-started/#download
BootstrapValidator 下载地址:http://www.bootcdn.cn/bootstrap-validator/
head 里引入 CSS
footer 里引入 JS
博主这里使用的是 Thymeleaf,所以有 th:href 和 th:src
HTML
JS
初始的
填写表单,验证效果如下
remote 远程调用可以参考这个
https://liuyanzhao.com/7844.html
一、下载和引用
Bootstrap 下载地址:https://v3.bootcss.com/getting-started/#download
BootstrapValidator 下载地址:http://www.bootcdn.cn/bootstrap-validator/
head 里引入 CSS
- <link rel="stylesheet" th:href="@{/common/bootstrap/v3/css/bootstrap.min.css}">
footer 里引入 JS
- <!-- jQuery -->
- <script th:src="@{/common/jquery/jquery.min.js}"></script>
- <!-- Bootstrap 3.3.7 -->
- <script th:src="@{/common/bootstrap/v3/js/bootstrap.min.js}"></script>
- <!--BootstrapValidator-->
- <script th:src="@{/common/bootstrap-validator/bootstrapValidator.js}"></script>
博主这里使用的是 Thymeleaf,所以有 th:href 和 th:src
二、开始使用
HTML
- <div class="form-group has-primary has-feedback">
- <label for="username" class="col-sm-3 control-label">
- 用户名 <span class="text-danger">*</span>
- </label>
- <div class="col-sm-8">
- <input type="text" id="username" name="username" class="form-control" >
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label for="name" class="col-sm-3 control-label">
- 昵称 <span class="text-danger">*</span>
- </label>
- <div class="col-sm-8">
- <input type="text" id="name" name="name" class="form-control" >
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label for="password" class="col-sm-3 control-label">
- 密码 <span class="text-danger">*</span>
- </label>
- <div class="col-sm-8">
- <input type="password" id="password" name="password" class="form-control" >
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label for="confirm_password" class="col-sm-3 control-label">
- 确认密码 <span class="text-danger">*</span>
- </label>
- <div class="col-sm-8">
- <input type="password" id="confirm_password" name="confirm_password" class="form-control">
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label class="col-sm-3 control-label">
- 角色 <span class="text-danger">*</span>
- </label>
- <div class="col-sm-8">
- <label class="checkbox-inline">
- <input type="checkbox" name="authorityId" value="1">管理员
- </label>
- <label class="checkbox-inline">
- <input type="checkbox" name="authorityId" value="2" checked>用户
- </label>
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label for="email" class="col-sm-3 control-label">
- Email <span class="text-danger">*</span>
- </label>
- <div class="col-sm-8">
- <input type="text" id="email" name="email" class="form-control" >
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label for="phone" class="col-sm-3 control-label">手机号码</label>
- <div class="col-sm-8">
- <input type="text" id="phone" name="phone" class="form-control" >
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label for="homepage" class="col-sm-3 control-label">个人主页</label>
- <div class="col-sm-8">
- <input type="text" id="homepage" name="homepage" class="form-control" >
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label for="profile" class="col-sm-3 control-label">简介</label>
- <div class="col-sm-8">
- <textarea class="form-control" rows="3" name="profile" id="profile"></textarea>
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <label class="col-sm-3 control-label">
- 状态 <span class="text-danger">*</span>
- </label>
- <div class="col-sm-8">
- <label class="radio-inline">
- <input type="radio" name="status" value="normal" checked>正常
- </label>
- <label class="radio-inline">
- <input type="radio" name="status" value="locked">锁定
- </label>
- </div>
- </div>
- <div class="form-group has-primary has-feedback">
- <div class="col-sm-offset-3 col-sm-8">
- <button type="submit" class="btn btn-primary">提交</button>
- </div>
- </div>
JS
- $(document).ready(function () {
- $("#userForm").bootstrapValidator({
- message: '输入值不合法',
- feedbackIcons: {
- valid: 'glyphicon glyphicon-ok',
- invalid: 'glyphicon glyphicon-remove',
- validating: 'glyphicon glyphicon-refresh'
- },
- fields: {
- username: {
- message: '用户名不合法',
- validators: {
- notEmpty: {
- message: '用户名不能为空'
- },
- stringLength: {
- min: 2,
- max: 20,
- message: '用户名为2到20个字符'
- },
- regexp: {
- regexp: /^[a-zA-Z0-9_.@]+$/,
- message: '用户名只能由字母、数字和下划线组成'
- },
- }
- },
- name: {
- message: '姓名不合法',
- validators: {
- notEmpty: {
- message: '姓名不能为空'
- },
- stringLength: {
- min: 2,
- max: 20,
- message: '昵称为2到20个字符'
- }
- }
- },
- password: {
- message: '密码不合法',
- validators: {
- notEmpty: {
- message: '密码不能为空'
- },
- stringLength: {
- min: 6,
- max: 100,
- message: '密码为6到100个字符'
- },
- regexp: {
- regexp: /^[a-zA-Z0-9_]+$/,
- message: '密码只能由字母、数字和下划线组成'
- }
- }
- },
- confirm_password: {
- message: '确认密码不合法',
- validators: {
- notEmpty: {
- message: '确认密码不能为空'
- },
- stringLength: {
- min: 6,
- max: 100,
- message: '请输入6到100个字符'
- },
- regexp: {
- regexp: /^[a-zA-Z0-9_]+$/,
- message: '确认只能由字母、数字和下划线组成'
- },
- identical: {
- //需要验证的field
- field: 'password',
- message: '两次密码输入不一致'
- }
- }
- },
- authorityId: {
- validators: {
- notEmpty: {
- message: '角色必选'
- }
- }
- },
- email: {
- validators: {
- notEmpty: {
- message: '邮箱地址不能为空'
- },
- emailAddress: {
- message: '请输入正确的邮箱地址'
- }
- }
- },
- phone: {
- validators: {
- // phone: {
- // country: 'CN',
- // message: "手机号不合法"
- // },
- stringlength: {
- min: 11,
- max: 11,
- message: '请输入11位手机号码'
- },
- regexp: {
- regexp: /^1[3|5|8]{1}[0-9]{9}$/,
- message: '请输入正确的手机号码'
- }
- }
- },
- homepage: {
- message: '个人主页不合法',
- validators: {
- uri: {
- message:'url不合法'
- },
- stringLength: {
- max: 50,
- message: '个人主页不要超过50个字符'
- },
- }
- },
- profile: {
- message: '简介不合法',
- validators: {
- stringLength: {
- max: 20,
- message: '简介不要超过50个字符'
- },
- }
- },
- status: {
- validators: {
- notEmpty: {
- message: '状态必选'
- }
- }
- },
- }
- });
- });
三、效果图
初始的
填写表单,验证效果如下
remote 远程调用可以参考这个
https://liuyanzhao.com/7844.html
您可以选择一种方式赞助本站
支付宝扫一扫赞助
微信钱包扫描赞助
赏