BootstrapValidator超详细教程(推荐)

yizhihongxing

BootstrapValidator超详细教程(推荐)

简介

BootstrapValidator 是一个针对 Bootstrap 的表单验证插件,能够在客户端对表单进行验证,使得用户在提交表单前能够方便地发现并修复错误。BootstrapValidator 具备以下特点:

  • 友好的 UI 体验
  • 支持多种校验方式,如正则表达式、长度等
  • 支持 Ajax 校验
  • 支持多语言

安装

安装 BootstrapValidator 可以通过多种方式,如下载压缩包或使用 npm 安装。这里我们介绍使用 CDN 引入的方式。

在 HTML 中引入 BootstrapValidator 的 CSS 和 JS 文件,以及 jQuery 和 Bootstrap 的文件:

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/css/bootstrapValidator.min.css" />
<script src="https://code.jquery.com/jquery-3.5.1.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.bootstrapvalidator/0.5.3/js/bootstrapValidator.min.js"></script>

使用

在 HTML 表单中添加 data 属性,如 data-bv-*,然后使用 JavaScript 初始化 BootstrapValidator。以下是一个简单的例子,展示了如何在表单中验证用户名和密码:

HTML 代码:

<form id="myForm">
  <div class="form-group">
    <label>用户名</label>
    <input type="text" class="form-control" name="username" required data-bv-remote url="/checkUserName" data-bv-remote-message="该用户名已被注册,请更换用户名" />
  </div>
  <div class="form-group">
    <label>密码</label>
    <input type="password" class="form-control" name="password" required data-bv-stringlength data-bv-stringlength-min="6" data-bv-stringlength-max="16" />
  </div>
  <button type="submit" class="btn btn-primary">提交</button>
</form>

JavaScript 代码:

$('#myForm').bootstrapValidator({
  fields: {
    username: {
      validators: {
        notEmpty: {
          message: '用户名不能为空'
        },
        remote: {
          message: '该用户名已被注册,请更换用户名',
          url: '/checkUserName',
          type: 'POST'
        }
      }
    },
    password: {
      validators: {
        notEmpty: {
          message: '密码不能为空'
        },
        stringLength: {
          min: 6,
          max: 16,
          message: '密码长度必须在 6 到 16 个字符之间'
        }
      }
    }
  }
})

上面的代码中,我们使用了 data-bv-* 属性实现了客户端验证的功能,并使用了 JavaScript 初始化了 BootstrapValidator,设置了验证规则和提示。其中,remote 规则可以进行 Ajax 校验,在 url 参数中设置远程校验的 URL。

示例

这里提供两个示例,一个是基本的表单验证,另一个是带有 Ajax 校验的表单验证。

基本的表单验证

通过 BootstrapValidator 官网 提供的基本示例,演示一个表单验证的案例。

HTML 代码:

<form id="myForm" class="form-horizontal">
  <div class="form-group">
    <label class="col-sm-3 control-label">Full name *</label>
    <div class="col-sm-7">
      <input type="text" class="form-control" name="firstName" placeholder="First name" />
    </div>
    <div class="col-sm-7 col-sm-offset-3">
      <input type="text" class="form-control" name="lastName" placeholder="Last name" />
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-3 control-label">Username *</label>
    <div class="col-sm-7">
      <input type="text" class="form-control" name="username" />
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-3 control-label">Email address *</label>
    <div class="col-sm-7">
      <input type="text" class="form-control" name="email" />
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-3 control-label">Password *</label>
    <div class="col-sm-7">
      <input type="password" class="form-control" name="password" />
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-3 control-label">Re-enter password *</label>
    <div class="col-sm-7">
      <input type="password" class="form-control" name="confirmPassword" />
    </div>
  </div>

  <div class="form-group">
    <div class="col-sm-offset-3 col-sm-9">
      <div class="checkbox">
        <label><input type="checkbox" name="agree" /> Agree with the terms and conditions</label>
      </div>
    </div>
  </div>

  <div class="form-group">
    <div class="col-sm-offset-3 col-sm-9">
      <button type="submit" class="btn btn-primary">Register</button>
    </div>
  </div>
</form>

JavaScript 代码:

$('#myForm').bootstrapValidator({
  message: 'This value is not valid',
  feedbackIcons: {
    valid: 'glyphicon glyphicon-ok',
    invalid: 'glyphicon glyphicon-remove',
    validating: 'glyphicon glyphicon-refresh'
  },
  fields: {
    firstName: {
      message: 'The first name is not valid',
      validators: {
        notEmpty: {
          message: 'The first name is required and cannot be empty'
        }
      }
    },
    lastName: {
      message: 'The last name is not valid',
      validators: {
        notEmpty: {
          message: 'The last name is required and cannot be empty'
        }
      }
    },
    username: {
      message: 'The username is not valid',
      validators: {
        notEmpty: {
          message: 'The username is required and cannot be empty'
        },
        stringLength: {
          min: 6,
          max: 30,
          message: 'The username must be more than 6 and less than 30 characters long'
        },
        regexp: {
          regexp: /^[a-zA-Z0-9_\.]+$/,
          message: 'The username can only consist of alphabetical, number, dot and underscore'
        }
      }
    },
    email: {
      validators: {
        notEmpty: {
          message: 'The email address is required and cannot be empty'
        },
        emailAddress: {
          message: 'The input is not a valid email address'
        }
      }
    },
    password: {
      validators: {
        notEmpty: {
          message: 'The password is required and cannot be empty'
        },
        stringLength: {
          min: 6,
          message: 'The password must be more than 6 characters long'
        },
        different: {
          field: 'username',
          message: 'The password cannot be the same as username'
        }
      }
    },
    confirmPassword: {
      validators: {
        notEmpty: {
          message: 'The confirm password is required and cannot be empty'
        },
        identical: {
          field: 'password',
          message: 'The password and confirm password are not the same'
        }
      }
    },
    agree: {
      validators: {
        notEmpty: {
          message: 'You must agree with the terms and conditions'
        }
      }
    }
  }
});

这个例子演示了输入框的实时验证、异步验证、即时反馈等功能。

带有 Ajax 校验的表单验证

在表单验证中,有时候需要通过 Ajax 获取数据并进行校验。这个例子演示了如何使用 BootstrapValidator 进行 Ajax 校验。

HTML 代码:

<form id="ajaxForm" method="post" class="form-horizontal" action="submitAjax.php">
  <div class="form-group">
    <label class="col-sm-3 control-label">用户名</label>
    <div class="col-sm-7">
      <input type="text" class="form-control" name="username" />
    </div>
  </div>

  <div class="form-group">
    <label class="col-sm-3 control-label">电子邮箱</label>
    <div class="col-sm-7">
      <input type="email" class="form-control" name="email" />
    </div>
  </div>

  <button type="submit" class="btn btn-primary">提交</button>
</form>

在 JavaScript 中发起 Ajax 校验,并通过 BootstrapValidator 进行验证:

var submitUrl = "submitAjax.php"; // 真实提交地址
$('#ajaxForm').bootstrapValidator({
  live: 'enabled',
  submitButtons: 'button[type="submit"]',
  message: 'This value is not valid',
  feedbackIcons: {
    valid: 'glyphicon glyphicon-ok',
    invalid: 'glyphicon glyphicon-remove',
    validating: 'glyphicon glyphicon-refresh'
  },
  fields: {
    username: {
      message: 'The username is not valid',
      validators: {
        notEmpty: {
          message: 'The username is required and cannot be empty'
        },
        remote: {
          message: 'The username is not available',
          url: submitUrl,
          type: 'POST',
          data: {
            checkType: 'username'
          }
        }
      }
    },
    email: {
      validators: {
        notEmpty: {
          message: 'The email address is required and cannot be empty'
        },
        emailAddress: {
          message: 'The input is not a valid email address'
        },
        remote: {
          message: 'This email has been registered',
          url: submitUrl,
          type: 'POST',
          data: {
            checkType: 'email'
          }
        }
      }
    }
  }
});

在 submitAjax.php 文件中获取数据并进行校验:

<?php

if($_SERVER['REQUEST_METHOD'] === 'POST'){
    // 获取查询类型
    $checkType = isset($_POST['checkType']) ? $_POST['checkType'] : '';

    if ($checkType === 'username') {// 校验用户名
        $username = isset($_POST['username']) ? $_POST['username'] : '';
        if ($username === 'admin') {
            echo json_encode(['valid'=>false]);
        } else {
            echo json_encode(['valid'=>true]);
        }
    } elseif ($checkType === 'email') {// 校验电子邮件
        $email = isset($_POST['email']) ? $_POST['email'] : '';
        if ($email === 'admin@qq.com') {
            echo json_encode(['valid'=>false]);
        } else {
            echo json_encode(['valid'=>true]);
        }
    } else {
        echo json_encode(['valid'=>true]);
    }
}
?>

这个例子演示了如何结合 BootstrapValidator 和后端配合使用。在实际项目中,这种方式非常常见。

本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:BootstrapValidator超详细教程(推荐) - Python技术站

(0)
上一篇 2023年6月10日
下一篇 2023年6月10日

相关文章

  • 使用CSS制作一个比较炫酷的页面切换动画

    下面是使用CSS制作比较炫酷的页面切换动画的攻略,包括具体步骤和示例说明。 攻略步骤 第一步:设置HTML和CSS基本结构 首先,需要设置HTML和CSS的基本结构。在HTML中,需要至少有两个页面容器,也就是两个div标签来包含每个页面的内容。在CSS中,需要设置页面容器的样式,包括width、height、position、overflow等属性,以及为…

    css 2023年6月10日
    00
  • jQuery实现切换页面过渡动画效果

    以下是详细的攻略: 1. 引入jQuery 首先,要在你的页面中引入jQuery库,可以在head标签中加入如下代码: <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script> 2. 创建两个页面 为了实…

    css 2023年6月10日
    00
  • 深入理解和应用css中Float属性

    深入理解和应用CSS中Float属性 概述 在 CSS 中,float 属性用于控制元素的浮动状态,常用于实现元素的排列,并且被广泛应用于响应式网页设计中。本篇文章将全面介绍 float 属性的使用方法以及相关的技巧和注意事项,帮助你更好地理解和应用 float 属性。 Float 属性的基本使用 Float 属性有三个常见的取值:left、right 和 …

    css 2023年6月10日
    00
  • css选择器四大类:基本、组合、属性、伪类

    CSS选择器四大类:基本、组合、属性、伪类攻略 基本选择器 基本选择器是最简单的选择器,包括四种类型: 1. 元素选择器 元素选择器是指根据元素的标签名称来选择网页中的元素,例如以下代码将选中 HTML 文档中所有的段落元素: p { color: red; } 2. 类选择器 类选择器是通过元素的 class 属性来进行选择(class 选择器以句点 . …

    css 2023年6月9日
    00
  • HTML 标签解释大全

    HTML 标签解释大全是一个结合了 HTML 标签及其语义和用法的一份详细说明文档。下面是对该攻略的完整讲解: 1. 概述 HTML(超文本标记语言)是一种标记语言,它用来描述网页的结构以及展示形式。HTML 标签是组成 HTML 网页的基本元素。HTML 标签包含标记名称和必要的属性,有些标签是成对出现,中间包含了所需要显示的信息。HTML 标签可以分为标…

    css 2023年6月9日
    00
  • bootstrap动态添加面包屑(breadcrumb)及其响应事件的方法

    Bootstrap是一个流行的前端框架,提供了大量的组件和样式,以便快速搭建专业的网页。其中面包屑(breadcrumb)是一种常用的导航方式,能够告诉用户当前所处的页面位置。本篇攻略将详细讲解如何在Bootstrap中动态添加面包屑以及如何响应面包屑的事件。 添加面包屑 首先,我们需要在HTML文件中添加包含面包屑的元素,如下所示: <nav ari…

    css 2023年6月11日
    00
  • vue中如何动态添加样式

    在Vue中,可以通过绑定class或style来动态添加样式。 绑定class 1. 对象语法 对象语法只能通过v-bind指令实现,需要传入一个对象,对象的键是类名,值是布尔值,当值为true时,类名生效,当值为false时,类名失效。 示例代码: <template> <div :class="{ active: isActi…

    css 2023年6月9日
    00
  • XHTML标准语法

    XHTML(Extensible HyperText Markup Language)是基于XML的下一代HTML标准,它和HTML一样可以用来定义文档的结构和内容,但是XHTML更加严格,它要求页面标记必须符合XML的语法规范,并且支持XML命名空间和样式表。 下面是XHTML标准语法的完整攻略: 1. 设置XHTML文档类型声明 在XHTML文档中,必须…

    css 2023年6月9日
    00
合作推广
合作推广
分享本页
返回顶部