Bootstrap是一套基于HTML、CSS、JavaScript开发的前端框架,具有响应式、移动端优先等特点,在开发过程中,表单验证是必不可少的环节。Bootstrap的表单验证功能十分强大,本文将从多个方面对其进行详细阐述。
一、必需元素
在表单验证过程中,主要包含以下元素:
- form表单:需要实现表单验证的页面表单元素
- 表单控件:需要进行验证的表单元素,如input、textarea、select等
- 验证规则:需要验证的规则
示例代码:
<form role="form" id="form" > <div class="form-group" > <label for="username" >用户名</label> <input type="text" class="form-control" id="username" name="username" > </div> <div class="form-group" > <label for="password" >密码</label> <input type="password" class="form-control" id="password" name="password" > </div> <button type="submit" class="btn btn-primary" >提交</button> </form>
二、验证规则
1. 必填项验证
必填项验证是最基本的一种验证,要求某些表单控件不能为空,否则无法提交表单。Bootstrap的表单控件中有一个属性为required,可以使用该属性进行验证。
示例代码:
<form role="form" id="form" > <div class="form-group" > <label for="username" >用户名</label> <input type="text" class="form-control" id="username" name="username" required > </div> <div class="form-group" > <label for="password" >密码</label> <input type="password" class="form-control" id="password" name="password" required > </div> <button type="submit" class="btn btn-primary" >提交</button> </form>
2. 格式验证
格式验证要求表单控件中输入的内容符合规定格式,比如邮箱、电话等格式,这种验证需要使用正则表达式。
示例代码:
<form role="form" id="form" > <div class="form-group" > <label for="email" >邮箱</label> <input type="email" class="form-control" id="email" name="email" pattern="[^@]*@[^@]*.[^@]*" > </div> <div class="form-group" > <label for="phone" >电话</label> <input type="tel" class="form-control" id="phone" name="phone" pattern="d{3}-d{8}|d{4}-d{7}" > </div> <button type="submit" class="btn btn-primary" >提交</button> </form>
3. 字符数验证
字符数验证要求控件输入的字符数符合规定范围,比如密码长度在6到10之间,可以通过minlength和maxlength属性进行控制。
示例代码:
<form role="form" id="form" > <div class="form-group" > <label for="password" >密码</label> <input type="password" class="form-control" id="password" name="password" minlength="6" maxlength="10" > </div> <button type="submit" class="btn btn-primary" >提交</button> </form>
4. 选项验证
选项验证要求必须选择其中一个选项进行验证,可以通过设置表单控件为radio或checkbox进行实现。
示例代码:
<form role="form" id="form" > <div class="form-group" > <label for="gender" >性别</label> <div class="radio" > <label> <input type="radio" name="gender" value="male" required >男 </label> <label> <input type="radio" name="gender" value="female" required >女 </label> </div> </div> <button type="submit" class="btn btn-primary" >提交</button> </form>
三、插件使用
BootStrap提供了很多插件,可以通过这些插件来进行表单验证。比如,bootstrap-validator插件提供了多种验证,可以根据需求进行选择使用。
1. 引入插件库
首先需要引入bootstrap-validator插件库:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <meta http-equiv="X-UA-Compatible" content="ie=edge"> <title>Bootstrap Form Validator</title> <!-- 引入CSS文件 --> <link rel="stylesheet" href="https://cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css"> </head> <body> <!-- 引入JS文件 --> <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://cdn.bootcss.com/bootstrap-validator/0.5.3/js/bootstrapValidator.min.js"></script> <!-- 引入本地JS文件 --> <script src="js/my_validator.js"></script> </body> </html>
2. 初始化插件
在JS文件中初始化bootstrap-validator插件:
$(document).ready(function() { $('#form').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: '用户名不能为空' }, stringLength: { min: 6, max: 18, message: '用户名长度必须在6到18之间' }, regexp: { regexp: /^[a-zA-Z0-9_]+$/, message: '用户名只能包含字母、数字和下划线' } } }, password: { message: 'The password is not valid', validators: { notEmpty: { message: '密码不能为空' }, stringLength: { min: 6, max: 18, message: '密码长度必须在6到18之间' }, regexp: { regexp: /^[a-zA-Z0-9_]+$/, message: '密码只能包含字母、数字和下划线' } } } } }); });
四、总结
本文介绍了Bootstrap表单验证的相关知识,包括必需元素、验证规则、插件使用等方面,并提供了相应的代码示例,帮助读者了解和学习Bootstrap表单验证,掌握相应的开发技能。