前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >bootstrapValidator的使用

bootstrapValidator的使用

作者头像
手撕代码八百里
发布2020-07-28 15:45:44
2.2K0
发布2020-07-28 15:45:44
举报
文章被收录于专栏:猿计划猿计划

一、页面引用插件介绍

一个比较详细的规则介绍的中文网站:

https://mrbird.cc/BootstrapValidator指南.html

https://www.zhuangyan.cn/BootstrapValidator-guide/

验证规则常用的正则表达式:

https://www.cnblogs.com/zxin/archive/2013/01/26/2877765.html

https://blog.csdn.net/IMW_MG/article/details/78705359

可以匹配整数和小数的正则

https://www.cnblogs.com/ZHF/archive/2012/06/26/2564009.html

1、CSS插件

代码语言:javascript
复制
<link rel="stylesheet" href="plug-in/survey/css/bootstrap/bootstrap-3.3.7.css" />

<!--
bootstrapValidator是一个基于bootstrap的表单验证组件  
-->
<link rel="stylesheet" href="plug-in/survey/dist/css/bootstrapValidator.css"/>

2、JS插件

代码语言:javascript
复制
 <script type="text/javascript" src="plug-in/survey/js/jquery/jquery-1.12.4.min.js"></script>

<script type="text/javascript" src="plug-in/survey/js/bootstrap/bootstrap-3.3.7.js"></script> 

<script src="plug-in/ace/datatables/dataTables.bootstrap.min.js"></script>

<!-- 表单验证组件 -->
<script type="text/javascript" src="plug-in/survey/dist/js/bootstrapValidator.js"></script>

二、通用方法介绍

注意:使用bootstrapValidator插件时,需要验证的数据,如:input类型的。必须由:form-group包裹,不然是无法验证的。

例如:这个格式是固定的

1、html书写格式

代码语言:javascript
复制
<--这种弄写法是不适用summit按钮提交数据,默认的form表单是使用input类型为sumbit的按钮提交的。因为需要ajax请求,所以禁止使用submit方式提交。然后手动触发验证,在提交   -->
<form id="zhform" onsubmit="return false" action="##" class="form-horizontal" >
    <--都要被 <div class="form-group" > </div> 包裹起来 不然是没有提示图标的效果 -->
    <div class="form-group" > 
        <--必须有name值,因为它是通过name值来验证的。-->
        <input type="text" class="number"  id="username" name="username"  />
    </div>
</form>

2、表单验证函数格式

代码语言:javascript
复制
/* 表单验证 */
function formValidator() {
    //#zhform  是一个from表单的id值
	$('#zhform').bootstrapValidator({
        //live: 'disabled', //验证方式,注释掉后三种都会生效
        container: '#messages', //在哪个地方显示提示语
        message: '验证未通过', //通用提示语
        feedbackIcons: {  //提示图标
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        }, 
        fields: {
        	要验证的名字(必须为input的name值): {
        		validators: {
        			notEmpty: { //不能为空
        				message: "经纬度不能为空!" //提示消息
        			},
        			digits: { //只能是数字
        				message: "经纬度只能为数字!"
        			},
        			between: { //控制在多少多少之间
                        min: -90,
                        max: 90,
                        message: '经度范围必须在: -90.0 和 90.0之间'
                    }
        		}
        	}
            ....
            	...	
            		...
            //更多规则请百度
            
        }
    })
	
	
	
}

2、初始化

代码语言:javascript
复制
$(function() {
    ...

    //初始化表单验证规则
    formValidator();

    ...
});

三、绕坑必备

1、一个只允许输入整数或者小数的input框。不能使用type=number,必须改为text才行(不然会一直验证不成功)。然后使用正则

代码语言:javascript
复制
regexp: { //使用正则
    regexp: /^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$/, //验证是不是数字
    message: '请输入整数或者小数'
}

2、

四、常用正则

1、验证是不是正整数

代码语言:javascript
复制
regexp: { //使用正则
    regexp: /^[1-9]\d*$/, //验证是不是正整数
    message: '输入整数'
}

2、验证是不是数字(整数、小数)

代码语言:javascript
复制
 regexp: { //使用正则
     regexp: /^[+-]?([0-9]*\.?[0-9]+|[0-9]+\.?[0-9]*)([eE][+-]?[0-9]+)?$/, //验证是不是数字
     message: '输入整数或者小数'
 }

3、验证正整数

代码语言:javascript
复制
 regexp: { //使用正则
     regexp: /^\d+$/, //验证是不是正整数
     message: '输入正整数'
 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2019-03-07 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 一、页面引用插件介绍
    • 1、CSS插件
      • 2、JS插件
      • 二、通用方法介绍
        • 1、html书写格式
          • 2、表单验证函数格式
          • 三、绕坑必备
          • 四、常用正则
            • 1、验证是不是正整数
              • 2、验证是不是数字(整数、小数)
                • 3、验证正整数
                领券
                问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档