How do I use Bootstrap 3 form validation when submit button is outside the form?

前端 未结 3 658
谎友^
谎友^ 2021-02-06 07:46

I\'m confused with the Bootstrap 3 implementation of form validation. I\'m new to Bootstrap, and I cannot seem to get the form validation working with a form that does not incl

3条回答
  •  执念已碎
    2021-02-06 08:06

    we need to use some kind of external library or custom code to add validation rules, which will enable us to add validation classes to form. I don't think there is other way around.

    Example:

    http://jsfiddle.net/mapb_1990/hTPY7/9/

    HTML:

    $

    JS:

    $('form').validate({
        rules: {
            firstname: {
                minlength: 3,
                maxlength: 15,
                required: true
            },
            lastname: {
                minlength: 3,
                maxlength: 15,
                required: true
            }
        },
        highlight: function(element) {
            $(element).closest('.form-group').addClass('has-error');
        },
        unhighlight: function(element) {
            $(element).closest('.form-group').removeClass('has-error');
        },
        errorElement: 'span',
        errorClass: 'help-block',
        errorPlacement: function(error, element) {
            if(element.parent('.input-group').length) {
                error.insertAfter(element.parent());
            } else {
                error.insertAfter(element);
            }
        }
    });
    

提交回复
热议问题