如何使用bootstrapValidator验证这两封电子邮件(相同)

时间:2017-10-15 08:19:48

标签: bootstrapvalidator

我正在使用bootstrapValidator(相同)检查两个电子邮件是否相同,它的工作正常,但我想在同一行中出现错误消息,现在它们分为两行。见https://screenshots.firefox.com/abfJ97LEjpAncVoW/localhost。我尝试了很多方式,并在谷歌找到但没有得到任何解决方案。以下是我的验证码:

$('#formid').bootstrapValidator({

        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-alert',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            firstmail: {
                validators: {
                    notEmpty: {
                        message: 'The email address is required and can\'t be empty'
                    },
                     emailAddress: {
                        message: 'The input is not a valid email address'
                    },
                    identical: {
                        field: '2ndmail',
                        message: 'The email and its confirm are not the same'
                    }
                }
            },
            2ndmail: {
                validators: {
                    notEmpty: {
                        message: 'The email address is required and can\'t be empty'
                    },
                     emailAddress: {
                        message: 'The input is not a valid email address'
                    },
                    identical: {
                        field: 'firstmail',
                        message: 'The email and its confirm are not the same'
                    }
                }
            },
            },

    })

1 个答案:

答案 0 :(得分:0)

以下代码解决了我的问题,我在$('#formId')下面添加了它.bootstrapValidator({.....});

$('#formid').bootstrapValidator({

feedbackIcons: {
    valid: 'glyphicon glyphicon-ok',
    invalid: 'glyphicon glyphicon-alert',
    validating: 'glyphicon glyphicon-refresh'
},
fields: {
    firstmail: {
        validators: {
            notEmpty: {
                message: 'The email address is required and can\'t be empty'
            },
             emailAddress: {
                message: 'The input is not a valid email address'
            },
            identical: {
                field: '2ndmail',
                message: 'The email and its confirm are not the same'
            }
        }
    },
    2ndmail: {
        validators: {
            notEmpty: {
                message: 'The email address is required and can\'t be empty'
            },
             emailAddress: {
                message: 'The input is not a valid email address'
            },
            identical: {
                field: 'firstmail',
                message: 'The email and its confirm are not the same'
            }
        }
    },
    },

})

.on('error.validator.bv', function(e, data) {
// $(e.target)    --> The field element
// data.bv        --> The BootstrapValidator instance
// data.field     --> The field name
// data.element   --> The field element
// data.validator --> The current validator name

data.element
    .data('bv.messages')
    // Hide all the messages
    .find('.help-block[data-bv-for="' + data.field + '"]').hide()
    // Show only message associated with current validator
    .filter('[data-bv-validator="' + data.validator + '"]').show();
});