`

bootstrapvalidator 验证框架

 
阅读更多

bootstrapvalidator 验证框架

https://github.com/nghuuphuoc/bootstrapvalidator/

1、resetForm

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

 

 2、属性校验

 

<input type="text" class="form-control" name="username"
                                   data-bv-message="The username is not valid"
                                   required data-bv-notempty-message="The username is required and cannot be empty"
                                   pattern="^[a-zA-Z0-9]+$" data-bv-regexp-message="The username can only consist of alphabetical and digits"
                                   data-bv-stringlength="true" data-bv-stringlength-min="6" data-bv-stringlength-max="30" data-bv-stringlength-message="The username must be more than 6 and less than 30 characters long"
                                   data-bv-different="true" data-bv-different-field="password" data-bv-different-message="The username and password cannot be the same as each other"
                                   data-bv-remote="true" data-bv-remote-url="remote.php" data-bv-remote-message="The username is not available"
                                    />
 
$(document).ready(function() {
        $('#defaultForm').bootstrapValidator();
    });
 3、Ajax提交

 

 

$(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: {
                    message: 'The username is not valid',
                    validators: {
                        notEmpty: {
                            message: 'The username is required and can\'t 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'
                        }
                    }
                }
            }
        })
        .on('success.form.bv', function(e) {
            // Prevent form submission
            e.preventDefault();

            // Get the form instance
            var $form = $(e.target);

            // Get the BootstrapValidator instance
            var bv = $form.data('bootstrapValidator');
			//Ajax 提交
            // Use Ajax to submit form data
            $.post($form.attr('action'), $form.serialize(), function(result) {
                console.log(result);
            }, 'json');
        });
});
 4、choice

 

 

 <form id="interviewForm" method="post" class="form-horizontal" action="target.php">
	<div class="form-group">
		<label class="col-lg-3 control-label">Programming Languages</label>
		<div class="col-lg-4">
			<div class="checkbox">
				<label>
					<input type="checkbox" name="languages[]" value="net" /> .Net
				</label>
			</div>
			<div class="checkbox">
				<label>
					<input type="checkbox" name="languages[]" value="java" /> Java
				</label>
			</div>
			<div class="checkbox">
				<label>
					<input type="checkbox" name="languages[]" value="c" /> C/C++
				</label>
			</div>
			<div class="checkbox">
				<label>
					<input type="checkbox" name="languages[]" value="php" /> PHP
				</label>
			</div>
			<div class="checkbox">
				<label>
					<input type="checkbox" name="languages[]" value="perl" /> Perl
				</label>
			</div>
			<div class="checkbox">
				<label>
					<input type="checkbox" name="languages[]" value="ruby" /> Ruby
				</label>
			</div>
			<div class="checkbox">
				<label>
					<input type="checkbox" name="languages[]" value="python" /> Python
				</label>
			</div>
			<div class="checkbox">
				<label>
					<input type="checkbox" name="languages[]" value="javascript" /> Javascript
				</label>
			</div>
		</div>
	</div>
</form>
 
$(document).ready(function() {
    $('#interviewForm').bootstrapValidator({
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            'languages[]': {
                validators: {
                    choice: {
                        min: 2,
                        max: 4,
                        message: 'Please choose %s - %s programming languages you are good at'
                    }
                }
            }
        }
    });
});
 5、不同的错误提示方式

 

  1. container: 'tooltip',
  2. container: '#errors',
  3. .on('error.field.bv'、.on('success.field.bv',、.on('success.form.bv' 事件

 

$(document).ready(function() {
    $('#defaultForm').bootstrapValidator({
        message: 'This value is not valid',
        container: 'tooltip',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
           
            password: {
                container: 'popover',
                validators: {
                    notEmpty: {
                        message: 'The password is required and cannot be empty'
                    },
                    different: {
                        field: 'username',
                        message: 'The password cannot be the same as username'
                    }
                }
            }
        }
    })
	.on('error.field.bv', function(e, data) {
            var messages = data.bv.getMessages(data.field);
            $('#errors').find('li[data-bv-for="' + data.field + '"]').remove();
            for (var i in messages) {
                $('<li/>').attr('data-bv-for', data.field).html(messages[i]).appendTo('#errors');
            }
            $('#errors').parents('.form-group').removeClass('hide');
        })
        .on('success.field.bv', function(e, data) {
            $('#errors').find('li[data-bv-for="' + data.field + '"]').remove();
        })
        .on('success.form.bv', function(e) {
            $('#errors')
                .html('')
                .parents('.form-group').addClass('hide');
        });
});
 

 

分享到:
评论

相关推荐

    bootstrapvalidator

    BootstrapValidator是一款广泛应用于Web开发的前端验证插件,它基于流行的Bootstrap框架构建,为开发者提供了简单易用的接口来实现表单验证功能。这个插件的主要目标是帮助开发者创建具有美观界面和强大验证功能的...

    BootstrapValidator-0.5.3表单验证

    BootstrapValidator是基于Bootstrap框架的一个强大的表单验证插件,版本0.5.3提供了丰富的功能和自定义选项,使得在Web应用中实现高效且美观的表单验证变得简单易行。这个压缩包包含了该插件的js文件和css样式文件,...

    bootstrapvalidator页面数据验证.zip

    BootstrapValidator是一款基于Bootstrap框架的前端数据验证插件,它提供了丰富的验证规则和优雅的提示样式,使得在网页表单中进行数据验证变得更加简单和直观。这个压缩包“bootstrapvalidator页面数据验证.zip”...

    bootstrapvalidator表单验证 +select2 拼音模糊匹配 demo

    BootstrapValidator是基于Bootstrap框架的一个强大的表单验证插件,它为HTML5表单提供了丰富的验证功能,能够确保用户输入的数据符合预设的规则。在"bootstrapvalidator表单验证 + select2 拼音模糊匹配 demo"这个...

    BootstrapValidator验证表单插件

    BootstrapValidator是一款基于Bootstrap框架的强大的表单验证插件,它为开发者提供了丰富的验证规则和灵活的自定义选项,使得在Web应用中实现复杂且用户友好的表单验证变得轻松便捷。这款插件能够帮助我们创建出更加...

    BOOTSTRAP VALIDATOR 源码下载

    Bootstrapvalidator-0.4.5是这个版本的文件名,这通常意味着它是该插件的一个稳定版本,可能包含了一些已知问题的修复和功能的改进。 以下是关于BOOTSTRAP VALIDATOR的一些关键知识点: 1. **基本使用**:...

    bootStrapValidator

    BootstrapValidator是一款基于Bootstrap框架的前端验证插件,用于在用户提交表单前验证输入的数据,确保数据的完整性和正确性。这款插件以其简洁的API和丰富的验证规则,深受前端开发者的喜爱。在Bootstrap的优雅...

    bootstrapValidator包,样例

    BootstrapValidator是基于Bootstrap框架的一个强大的验证插件,用于在前端实现表单数据的实时验证。这个包中的"bootstrapvalidator-master"文件夹很可能包含了该插件的源码、示例、文档和其他相关资源。以下是关于...

    好用的bootstrapvalidator表单验证

    BootstrapValidator是一款基于Bootstrap框架的强大的表单验证插件,它为开发者提供了丰富的验证规则和便捷的验证机制,使得创建复杂的Web表单变得轻而易举。这个插件通过优雅的UI设计和灵活的配置选项,使得表单验证...

    bootstrapvalidator是一款简单实用的Bootstrap3表单验证jQuery插件

    BootstrapValidator是针对Bootstrap3框架的一款高效且易于使用的jQuery表单验证插件,它极大地简化了在Web应用中实现复杂表单验证的过程。这款插件利用HTML5的数据属性(data attributes)来设定验证规则,使得...

    bootstrapvalidator 表单验证

    BootstrapValidator是一款基于Bootstrap框架的强大的表单验证插件,它为HTML5表单提供了一种美观、高效的验证机制。在Web应用中,确保用户输入的数据合法性和准确性是至关重要的,BootstrapValidator为此提供了一...

    bootstrap表单验证插件bootstrapvalidator

    BootstrapValidator是一款基于Bootstrap框架的强大的表单验证插件,它为开发者提供了便捷的方式来验证用户在网页表单中输入的数据。这个插件通过简单的配置选项,可以让开发者轻松地为各种表单字段添加验证规则,...

    bootstrapValidator表单验证插件

    BootstrapValidator是一款基于Bootstrap框架的强大的表单验证插件,它为开发者提供了丰富的验证规则和灵活的自定义选项,使得在Web应用中实现高效且用户体验优良的表单验证变得轻松便捷。这款插件不仅外观与...

    bootstrap、bootstrapValidator和jquery相关的部分js和css

    BootstrapValidator的CSS和JS文件(如`bootstrapValidator.css`和`bootstrapValidator.js`)则负责表单验证的样式和逻辑。为了提高页面加载速度,通常会将这些文件进行合并和压缩,如`bootstrap.min.css`和`...

    bootstrapvalidator合集.zip

    BootstrapValidator是一款基于Bootstrap框架的验证插件,用于在前端实现高效、美观的表单验证功能。这个资源包“bootstrapvalidator合集.zip”包含了与BootstrapValidator相关的多个组件和库,为开发者提供了一站式...

    bootstrap validator 使用简介

    Bootstrap Validator是一款基于Bootstrap框架的前端验证插件,用于在用户提交表单前验证输入的数据,确保数据的完整性和正确性。这款插件以其简洁的API和丰富的自定义选项,为开发者提供了高效且易于使用的表单验证...

    bootstrap,bootstrapValidator实现登录(login)页面

    BootstrapValidator是基于Bootstrap的一个强大的验证插件,它能够对用户输入的数据进行实时验证,确保数据的完整性和准确性。这个插件提供了多种内置验证规则,如非空、邮箱格式、手机号码等,并且可以自定义验证...

Global site tag (gtag.js) - Google Analytics