首页 > 其他分享 >基于Bootstrap的强大jQuery表单验证插件

基于Bootstrap的强大jQuery表单验证插件

时间:2024-12-03 12:22:13浏览次数:3  
标签:jQuery 插件 form 验证 message Bootstrap field div class

预览  下载

formvalidation是一款功能非常强大的基于Bootstrap的JQUERY表单验证插件。该jQuery表单验证插件内置了16种表单验证器,你也可以通过Bootstrap Validator's APIs写自己的表单验证器。该表单验证插件的可用验证器有:

  • between:检测输入的值是否在两个指定的值之间。
  • callback:通过回调函数返回验证信息。
  • creditCard:验证信用卡格式。
  • different:如果输入值和给定的值不同返回true
  • digits:如果输入的值只包含数字返回true
  • emailAddress:验证电子邮件格式是否有效。
  • greaterThan:如果输入的值大于或等于指定的值返回true
  • hexColor:验证一个hex格式的颜色值是否有效。
  • identical:验证输入的值是否和指定字段的值相同。
  • lessThan:如果输入的值小于或等于指定的值返回true
  • notEmpty:检测字段是否为空。
  • regexp:检测输入值是否和指定的javascript正则表达式匹配。
  • remote:通过AJAX请求来执行远程代码。
  • stringLength:验证字符串的长度。
  • uri:验证URL地址是否有效。
  • usZipCode:验证美国的邮政编码格式。

 

 使用方法

使用这个表单验证插件首先要引入必要的js和css文件。jQuery要求1.9.1+以上的版本。

<script src="jquery/1.10.2/jquery.min.js"></script> <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/css/bootstrap.min.css"> <script src="http://netdna.bootstrapcdn.com/bootstrap/3.0.2/js/bootstrap.min.js"></script> <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script> <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>              
 HTML结构

该表单验证插件的最基本例子的HTML结果如下:

<form id="defaultForm" method="post" class="form-horizontal">   <div class="form-group">     <label class="col-lg-3 control-label">Username</label>     <div class="col-lg-5">       <input type="text" class="form-control" name="username" />     </div>   </div>   <div class="form-group">     <label class="col-lg-3 control-label">Email address</label>     <div class="col-lg-5">       <input type="text" class="form-control" name="email" />     </div>   </div>   <div class="form-group">     <label class="col-lg-3 control-label">Password</label>     <div class="col-lg-5">       <input type="password" class="form-control" name="password" />     </div>   </div>   <div class="form-group">     <label class="col-lg-3 control-label">Retype password</label>     <div class="col-lg-5">       <input type="password" class="form-control" name="confirmPassword" />     </div>   </div>   <div class="form-group">     <label class="col-lg-3 control-label" id="captchaOperation"></label>     <div class="col-lg-2">       <input type="text" class="form-control" name="captcha" />     </div>   </div>   <div class="form-group">     <div class="col-lg-9 col-lg-offset-3">       <button type="submit" class="btn btn-primary">Sign up</button>     </div>   </div> </form>               
 JAVASCRIPT

在页面加载完毕之后,通过下面的方法来初始化该表单验证插件:

<script type="text/javascript">   $(document).ready(function() {   // Generate a simple captcha   function randomNumber(min, max) {   return Math.floor(Math.random() * (max - min + 1) + min);   };   $('#captchaOperation').html([randomNumber(1, 100), '+', randomNumber(1, 200), '='].join(' '));     $('#defaultForm').bootstrapValidator({     message: 'This value is not valid',       fields: {       username: {       message: 'The username is not valid',       validators: {       notEmpty: {       message: 'The username is required and can\'t 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'         },         different: {         field: 'password',         message: 'The username and password can\'t be the same as each other'         }       }     },     email: {       validators: {         notEmpty: {           message: 'The email address is required and can\'t be empty'         },         emailAddress: {           message: 'The input is not a valid email address'         }       }     },     password: {     validators: {       notEmpty: {           message: 'The password is required and can\'t be empty'         },         identical: {           field: 'confirmPassword',           message: 'The password and its confirm are not the same'         },         different: {           field: 'username',           message: 'The password can\'t be the same as username'         }       }     },     confirmPassword: {       validators: {         notEmpty: {         message: 'The confirm password is required and can\'t be empty'         },         identical: {         field: 'password',         message: 'The password and its confirm are not the same'         },         different: {         field: 'username',         message: 'The password can\'t be the same as username'         }       }     },     captcha: {       validators: {         callback: {           message: 'Wrong answer',           callback: function(value, validator) {           var items = $('#captchaOperation').html().split(' '), sum = parseInt(items[0]) + parseInt(items[2]);           return value == sum;           }         }       }     }   }   }); }); </script>

 配置参数

该表单验证插件的默认参数配置如下:

// The first invalid field will be focused automatically autoFocus: true,   // Support declarative usage (setting options via HTML 5 attributes) // Setting to false can improve the performance declarative: true,   // The form CSS class elementClass: 'fv-form',   // Use custom event name to avoid window.onerror being invoked by jQuery // See #630 events: {   // Support backward   formInit: 'init.form.fv',   formError: 'err.form.fv',   formSuccess: 'success.form.fv',   fieldAdded: 'added.field.fv',   fieldRemoved: 'removed.field.fv',   fieldInit: 'init.field.fv',   fieldError: 'err.field.fv',   fieldSuccess: 'success.field.fv',   fieldStatus: 'status.field.fv',   localeChanged: 'changed.locale.fv',   validatorError: 'err.validator.fv',   validatorSuccess: 'success.validator.fv' },   // Indicate fields which won't be validated // By default, the plugin will not validate the following kind of fields: // - disabled // - hidden // - invisible // // The setting consists of jQuery filters. Accept 3 formats: // - A string. Use a comma to separate filter // - An array. Each element is a filter // - An array. Each element can be a callback function //    function($field, validator) { //  $field is jQuery object representing the field element //  validator is the BootstrapValidator instance //  return true or false; //    } // // The 3 following settings are equivalent: // // 1) ':disabled, :hidden, :not(:visible)' // 2) [':disabled', ':hidden', ':not(:visible)'] // 3) [':disabled', ':hidden', function($field) { //return !$field.is(':visible'); //  }] excluded: [':disabled', ':hidden', ':not(:visible)'],   // Map the field name with validator rules fields: null,   // Live validating option // Can be one of 3 values: // - enabled: The plugin validates fields as soon as they are changed // - disabled: Disable the live validating. The error messages are only shown after the form is submitted // - submitted: The live validating is enabled after the form is submitted live: 'enabled',   // Locale in the format of languagecode_COUNTRYCODE locale: 'en_US',   // Default invalid message message: 'This value is not valid',   // The field will not be live validated if its length is less than this number of characters threshold: null,   // Whether to be verbose when validating a field or not. // Possible values: // - true:  when a field has multiple validators, all of them will be checked, and respectively - if errors occur in //  multiple validators, all of them will be displayed to the user // - false: when a field has multiple validators, validation for this field will be terminated upon the first encountered error. //  Thus, only the very first error message related to this field will be displayed to the user verbose: true,   // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // These options mostly are overridden by specific framework // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~   button: {   // The submit buttons selector   // These buttons will be disabled to prevent the valid form from multiple submissions   selector: '[type="submit"]',     // The disabled class   disabled: '' },   control: {   // The CSS class for valid control   valid: '',     // The CSS class for invalid control   invalid: '' },   err: {   // The CSS class of each message element   clazz: '',     // The error messages container. It can be:   // - 'tooltip' if you want to use Bootstrap tooltip to show error messages   // - 'popover' if you want to use Bootstrap popover to show error messages   // - a CSS selector indicating the container   // In the first two cases, since the tooltip/popover should be small enough, the plugin only shows only one error message   // You also can define the message container for particular field   container: null,     // Used to determine where the messages are placed   parent: null },   // Shows ok/error/loading icons based on the field validity. icon: {   valid: null,   invalid: null,   validating: null,   feedback: '' },   row: {   // The CSS selector for indicating the element consists of the field   // You should adjust this option if your form group consists of many fields which not all of them need to be validated   selector: null,   valid: '',   invalid: '',   feedback: '' }  

 

 

标签:jQuery,插件,form,验证,message,Bootstrap,field,div,class
From: https://www.cnblogs.com/liylllove/p/18583820

相关文章

  • Neo4j APOC-01-图数据库 apoc 插件介绍
    neo4japoc系列Neo4jAPOC-01-图数据库apoc插件介绍Neo4jAPOC-01-图数据库apoc插件安装neo4jonwindows10Neo4jAPOC-03-图数据库apoc实战使用使用Neo4jAPOC-04-图数据库apoc实战使用使用apoc.path.spanningTree最小生成树neo4j的apocNeo4j的APOC(Awesome......
  • jQuery简单实用的响应式固定侧边栏插件
    Bamboo.js是一款简单实用JQUERY响应式固定侧边栏插件。该插件使用简单固定侧边栏结构,带有固定位置的标题,侧边栏不会随页面而滚动。你可以为创建设置一个breakpoint,当屏幕尺寸小于这个breakpoint的时候侧边栏会隐藏,同时显示一个汉堡图标作为菜单的导航图标。演示  下载 使......
  • PbootCMS模板后台编辑器无法上传图片,提示“后端配置项没有正常加载,上传插件不能正常使
    当您在使用PbootCMS模板后台编辑器时,如果遇到无法上传图片,并且提示“后端配置项没有正常加载,上传插件不能正常使用!”的问题,通常是由于后端配置项返回格式出错导致的。以下是详细的解决步骤:检查时区设置:这个问题的一个常见原因是时区设置不正确。在Linux环境下,时区设置是区分......
  • jQuery轻量级Lightbox插件-Colorbox
    演示       下载 ColorBox是一款功能强大的轻量级Jquery Lightbox插件。ColorBox支持图片展示、图片分组、幻灯片、行内样式和iframe内容。该lightbox插件的兼容性极好,可以兼容IE7+的IE浏览器。它的特点有:支持图片展示、图片分组、幻灯片、行内样式和ifram......
  • Bootstrap-Flask的完整开发示例
    下面是一个完整的Bootstrap-Flask示例项目,展示如何结合Flask-WTF和Bootstrap-Flask构建一个带有表单的网页。项目功能1.提供一个简单的用户注册表单。2.使用Flask-WTF定义表单。3.使用Bootstrap-Flask自动为表单和字段应用Bootstrap样式。目录结构boots......
  • 【Unity 插件】Visual State Machine 通过图形化的界面帮助开发者设计和管理复杂的状
    VisualStateMachine是一款用于Unity编辑器中的插件,旨在通过图形化的界面帮助开发者设计和管理复杂的状态机逻辑。它为Unity提供了一个直观的拖拽式状态机系统,可以用来控制角色行为、AI、动画、UI交互等各种状态转换。主要特点:图形化界面:使用拖拽式界面来创建和管理......
  • 使用vue3-json-excel插件数据过长生成的数据变为科学计数法
    存在的问题:借用vue3-json-excel插件导出的xls的tagID这一项数据过长出现科学技术法。方法1.网上给出的办法是将长数字转换为字符串。我的数据tagID这个数据接口返回来的本就是字符串。所以改方法不行......
  • jQuery和CSS3折叠卡片式下拉列表框特效
    这是一款使用JQUERY和CSS3制作的效果非常炫酷的折叠卡片式下拉列表框特效。该下拉列表框特效将每一个列表项都制作为卡片的样式,打开和关闭它有打开和关闭百叶窗的感觉,效果非常不错。预览  下载  使用方法 HTML结构该下拉列表框特效的列表项使用一个无序列表来制作,用......
  • 基于Bootstrap3的简单柱状图表插件
    jchart是一款简单小巧的基于Bootstrap3.x的jquery柱状图表插件。该柱状图片表插件通过简单的设置,就可以生成非常漂亮的水平柱状图,并带有水平和垂直标签以及图表的头部和尾部。 在线演示  下载使用方法该jQuery柱状图插件可以通过javascript来调用,也可以直接使用HTML标签......
  • geocodeCN:一个批量将地址转为地理坐标的插件tF
    目录*1.介绍2.使用步骤:2.1安装2.2配置2.3坐标匹配2.4生成图层2.5导出为CSV:蓝猫加速器配置下载3.说明1.介绍这是一个QGIS插件,主要用于批量地理编码,即将地址转为坐标。它本身附带用户操作界面(GUI),开箱即用,即便你不会编程,也能轻松上手。其特点如下:支持批......