bootstrap:能够增加兼容性的强大框架.
因为项目需要数据验证,看bootstrapValidator 还不错,就上手一直,完美兼容,话不多说。
需要引用css:
bootstrap.min.css
bootstrapValidator.min.css
js:
jQuery-1.10.2.min.js
bootstrap.min.js
bootstrapValidator.min.js
以上这些都是必须的。
先上个简单的例子,只要导入相应的文件可以直接运行:
!DOCTYPE html html head title Using Ajax to submit data /title link rel="stylesheet" href="css/bootstrap.css" rel="external nofollow" / link rel="stylesheet" href="css/bootstrapValidator.css" rel="external nofollow" / script type="text/javascript" src="js/jquery-1.10.2.min.js" /script script type="text/javascript" src="js/bootstrap.min.js" /script script type="text/javascript" src="js/bootstrapValidator.js" /script /head body div !-- class都是bootstrap定义好的样式,验证是根据input中的name值 -- " !-- 下面这个div必须要有,插件根据这个进行添加提示 -- div label Username /label div input type="text" name="username" / /div /div div label Email address /label div input type="text" name="email" / /div /div div label Password /label div input type="password" name="password" / /div /div div div button type="submit" Sign up /button /div /div /form /div script type="text/javascript" $(document).ready(function() { * 下面是进行插件初始化 * 你只需传入相应的键值对 * */ $('#defaultForm').bootstrapValidator({ message: 'This value is not valid', feedbackIcons: {/*输入框不同状态,显示图片的样式*/ valid: 'glyphicon glyphicon-ok', invalid: 'glyphicon glyphicon-remove', validating: 'glyphicon glyphicon-refresh' fields: {/*验证*/ username: {/*键名username和input name值对应*/ message: 'The username is not valid', validators: { notEmpty: {/*非空提示*/ message: '用户名不能为空' stringLength: {/*长度提示*/ min: 6, max: 30, message: '用户名长度必须在6到30之间' }/*最后一个没有逗号*/ password: { message:'密码无效', validators: { notEmpty: { message: '密码不能为空' stringLength: { min: 6, max: 30, message: '用户名长度必须在6到30之间' email: { validators: { notEmpty: { message: 'The email address is required and can\'t be empty' emailAddress: { message: 'The input is not a valid email address' /script /body /html
当然,以上都是插件写好的规则,如果想自己加匹配规则怎么办呢?
如下只要在input相对应的键值中加入一个regexp:{}键值对(在上面的js基础上修改)
username: {/*键名和input name值对应*/ message: 'The username is not valid', validators: { notEmpty: {/*非空提示*/ message: '用户名不能为空' regexp: {/* 只需加此键值对,包含正则表达式,和提示 */ regexp: /^[a-zA-Z0-9_\.]+$/, message: '只能是数字和字母_.' stringLength: {/*长度提示*/ min: 6, max: 30, message: '用户名长度必须在6到30之间' }/*最后一个没有逗号*/ },
以上所述是小编给大家介绍的BootStrapValidator初使用教程详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对凡科网站的支持!
Copyright © 广州凡科互联网科技有限公司 版权所有 粤ICP备10235580号
全国服务电话:4000-399-000 传真:021-45545458
公司地址:广州市海珠区工业大道北67号凤凰创意园