BootStrap Validator入门

目录

官网

使用效果

认识bootstrapvalidator

初级用法

简单使用


官网

官网http://bootstrapvalidator.com/

源码下载地址https://github.com/nghuuphuoc/bootstrapvalidator

使用效果

 

认识bootstrapvalidator

来看bootstrapvalidator的描述:The best jQuery plugin to validate form fields. Designed to use with Bootstrap 3。从描述中我们就可以知道它至少需要jQuery、bootstrap的支持。我们来看看bootstrapvalidator的代码结构

├── demo
├── dist
│   ├── css
│   └── js
│       └── language
├── screenshots
├── src
│   ├── css
│   └── js
│       ├── language
│       └── validator
├── test
│   └── spec
│       └── validator
└── vendor
    ├── bootstrap
    │   ├── css
    │   ├── fonts
    │   └── js
    ├── jasmine
    └── jquery

demo中是各种用法示例

dist是编译后的结果

screenshot是校验的效果截图

src是源码目录

test是各个校验器的单元测试实现

vender是依赖库,其中bootstrap和jquery是bootstrapvalidator的依赖库,jasmine是单元测试的依赖库

 

初级用法

引入插件


<link rel="stylesheet" href="/path/to/bootstrap/css/bootstrap.css"/>
<link rel="stylesheet" href="/path/to/dist/css/bootstrapValidator.min.css"/>
<script type="text/javascript" src="/path/to/jquery/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="/path/to/bootstrap/js/bootstrap.min.js"></script>

<!-- Either use the compressed version (recommended in the production site) -->
<script type="text/javascript" src="/path/to/dist/js/bootstrapValidator.min.js"></script>
<!-- Or use the original one with all validators included -->
<script type="text/javascript" src="/path/to/dist/js/bootstrapValidator.js"></script>
<!-- Or use the plugin with required validators -->
<script type="text/javascript" src="/path/to/src/js/bootstrapValidator.js"></script>
<script type="text/javascript" src="/path/to/src/js/validator/...validator..."></script>

<!-- If you want to use the default message translated in the language package, then finally include it-->
<script type="text/javascript" src="/path/to/src/js/language/languagecode_COUNTRYCODE.js"></script>

如果语言是英文环境,不需要引用语言包,因为默认语言包是英文

由于bootstrapvalidator是基于bootstrap设计的,所以必须使用bootstrap风格的表格

如果不是bootstrap风格表格的话,会报以下错误

// Chrome
Uncaught RangeError: Maximum call stack size exceeded

// Firefox
Too much recursion error

命名注意点:

 submitresetlengthmethod不要使用它们给你的表单元素的name或id取值

 

简单使用

<form class="registerForm">
    <div class="form-group">
        <label>Username</label>
        <input type="text" class="form-control" name="username" />
    </div>

    <div class="form-group">
        <label>Email address</label>
        <input type="text" class="form-control" name="email" />
    </div>
</form>
$(document).ready(function() {
    $('.registerForm').bootstrapValidator({
        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'
                    },
                    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 and underscore'
                    }
                }
            },
            email: {
                validators: {
                    notEmpty: {
                        message: 'The email is required and cannot be empty'
                    },
                    emailAddress: {
                        message: 'The input is not a valid email address'
                    }
                }
            }
        }
    });
});

内置校验器

示例:\demo\validators.html

No.NameDescription
1base64Validate a base64 encoded string
2betweenCheck if the input value is between (strictly or not) two given numbers
3callbackReturn the validity from a callback method
4choiceCheck if the number of checked boxes are less or more than a given number
5creditCardValidate a credit card number
6cusipValidate a CUSIP
7cvvValidate a CVV number
8dateValidate date
9differentReturn true if the input value is different with given field's value
10digitsReturn true if the value contains only digits
11eanValidate an EAN (International Article Number)
12emailAddressValidate an email address
13fileValidate file
14greaterThanReturn true if the value is greater than or equals to given number
15gridValidate a GRId (Global Release Identifier)
16hexValidate a hexadecimal number
17hexColorValidate a hex color
18ibanValidate an International Bank Account Number (IBAN)
19idValidate identification number
20identicalCheck if the value is the same as one of particular field
21imeiValidate an IMEI (International Mobile Station Equipment Identity)
22imoValidate an IMO (International Maritime Organization)
23integerValidate an integer number
24ipValidate an IP address. Support both IPv4 and IPv6
25isbnValidate an ISBN (International Standard Book Number). Support both ISBN 10 and ISBN 13
26isinValidate an ISIN (International Securities Identification Number)
27ismnValidate an ISMN (International Standard Music Number)
28issnValidate an ISSN (International Standard Serial Number)
29lessThanReturn true if the value is less than or equals to given number
30macValidate a MAC address
31meidValidate a MEID (mobile equipment identifier)
32notEmptyCheck if the value is empty
33numericCheck if the value is numeric
34phoneValidate a phone number
35regexpCheck if the value matches given Javascript regular expression
36remotePerform remote checking via Ajax request
37rtnValidate a RTN (Routing transit number)
38sedolValidate a SEDOL (Stock Exchange Daily Official List)
39sirenValidate a Siren number
40siretValidate a Siret number
41stepCheck if the value is valid step one
42stringCaseCheck if a string is a lower or upper case one
43stringLengthValidate the length of a string
44uriValidate an URL address
45uuidValidate an UUID, support v3, v4, v5
46vatValidate VAT number
47vinValidate an US VIN (Vehicle Identification Number)
48zipCodeValidate a zip code

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值