BootstrapValidator验证表单插件

来源:https://www.sucaihuo.com/js/486.html 2015-09-07 15:21浏览(6573) 收藏

本文演示了bootstrap表单验证,压缩包里有多种演示方法。js如何验证表单,validate验证用户表单输入插件,判断格式是否正确
BootstrapValidator验证表单插件
分类:表单代码 > 表单插件 难易:中级
下载资源 下载积分: 40 积分

表单HTML代码

<form id="defaultForm" method="post" class="form-horizontal" action="target.php">
    <div class="form-group">
        <label class="col-lg-3 control-label">用户名</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">邮箱</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">生日</label>
        <div class="col-lg-5">
            <input type="text" class="form-control" name="birthday" /> (YYYY/MM/DD)
        </div>
    </div>

    <div class="form-group">
        <div class="col-lg-9 col-lg-offset-3">
            <button type="submit" class="btn btn-primary" name="signup" value="Sign up">提交</button>
            <button type="button" class="btn btn-info" id="validateBtn">自动验证</button>
            <button type="button" class="btn btn-info" id="resetBtn">重置表单</button>
        </div>
    </div>
</form>

引入bootstrap和bootstrapValidator表单验证插件

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

表单验证

$('#defaultForm').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, dot and underscore'
                },
                remote: {
                    url: 'remote.php',
                    message: 'The username is not available'
                },
                different: {
                    field: 'password',
                    message: 'The username and password cannot be the same as each other'
                }
            }
        },
        email: {
            validators: {
                emailAddress: {
                    message: 'The input is not a valid email address'
                }
            }
        },
    }
});

表单提交

$('#validateBtn').click(function() {
     $('#defaultForm').bootstrapValidator('validate');
});

表单重置

$('#resetBtn').click(function() {
     $('#defaultForm').data('bootstrapValidator').resetForm(true);
});

BootstrapValidator两种提交方法:

var bootstrapValidator = $(form).data('bootstrapValidator');
bootstrapValidator.methodName(parameters)

或者

$(form).bootstrapValidator(methodName, parameters);

第一种方法主要是返回 BootstrapValidator同时第二个实例,总是返回jQuery对象表示形式。

所以方法拓展如下:

// 第一种方法
$(form)
    .data('bootstrapValidator')
    .updateStatus('birthday', 'NOT_VALIDATED')
    .validateField('birthday');

//  第二种方法
$(form)
    .bootstrapValidator('updateStatus', 'birthday', 'NOT_VALIDATED')
    .bootstrapValidator('validateField', 'birthday');

bootstrapValidator中文API

参数 描述 默认值
defaultSubmit 默认提交表单 -
disableSubmitButtons 禁用或启用提交按钮 -
enableFieldValidators 启用/禁用所有给定的字段验证器 (如果 true,使字段验证器。如果 false禁用字段验证器) -
getFieldElements 检索字段元素的名字 -
isValid 返回 true如果所有的表单字段是有效的。否则,返回 false. -
resetForm 重置表单 -
updateElementStatus 更新验证给定元素的结果 -
updateStatus(field, status, validatorName) 更新为给定字段验证器的结果,status可以 NOT_VALIDATED, VALIDATING, INVALID或 VALID,validatorName 字符串 验证器的名称。如果 null所有验证器,更新方法有效性的结果 -
评论0
头像

系统已开启自动识别垃圾评论机制,识别到的自动封号,下载出错或者资源有问题请联系全栈客服QQ 1915635791

1 2