Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call validation on a button click event thats not a submit type but a button type and without a form?

I have been trying to figure this out, I know validation can be fired within a form as long as the button is of type submit. But I have a few other tabs on the view and when I use submit, it causes a posback and goes back to the first tab. I don't want that to happen so I put my validation in a function and changed the button type to a button and removed the form tag and tried calling the validation function on the button click event and its not firing. Is this possible to do without using a form tag?

My code is this...

    $(document).ready(function () {
    $("#btnUpdateModerator").click(function () {
        ValidateIt();
    });
    ValidateIt();
});
function ValidateIt() {
    var validator = $("#Users").bootstrapValidator({
        feedbackIcons: {
            valid: "glyphicon glyphicon-ok",
            invalid: "glyphicon glyphicon-remove",
            validating: "glyphicon glyphicon-refresh"
        },
        fields: {
            DealerUsername: {
                message: "Username is required",
                validators: {
                    notEmpty: {
                        message: "Please Provide Username"
                    }
                }
            },
            email: {
                message: "Email address is required",
                validators: {
                    notEmpty: {
                        message: "Please provide Email address"
                    },
                    emailAddress: {
                        message: "Email address was invalid"
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: "Password is required"
                    },
                    stringLength: {
                        min: 6,
                        message: "Password must be at least 6 characters long"
                    },
                    different: {
                        field: "email",
                        message: "Email and Password cannot match"
                    }
                }
            },
            confirmPassword: {
                validators: {
                    notEmpty: {
                        message: "Confirm Password is required"
                    },
                    identical: {
                        field: "password",
                        message: "Password and Confirmation Password must match"
                    }
                }
            },
            department: {
                validators: {
                    notEmpty: {
                        message: "Department is Required"
                    }
                }
            }
        }
    });
}
like image 787
Chris Avatar asked Sep 05 '15 23:09

Chris


People also ask

How do you validate a form field before submitting?

on('change', function() { $(this). valid(); // force a validation test on this field }); This will force the validation on element before form is submitted and you will get the validation message.

How do I validate a button in HTML?

The formnovalidate attribute is a boolean attribute. When present, it specifies that the form-data should not be validated on submission. This attribute overrides the form's novalidate attribute. The formnovalidate attribute is only used for buttons with type="submit" .

How do I validate a submit button?

The correct way is onclick="validate_activity();" and return false in validata_activity function, which will stop the default submit action.

How do you validate fields in a form?

Basic Validation − First of all, the form must be checked to make sure all the mandatory fields are filled in. It would require just a loop through each field in the form and check for data. Data Format Validation − Secondly, the data that is entered must be checked for correct form and value.


Video Answer


2 Answers

I think you need to keep the form element, but set the button type to "button" instead of "submit". You can then manually validate the form using the bootstrap validator's "validate" method.

Assuming your form element has the id of "Users", you should be able to change your code to:

$(document).ready(function () {
    $("#btnUpdateModerator").click(function () {
        $('#Users').bootstrapValidator('validate');
    });
    ValidateIt();
});

Note: The ValidateIt() function as you have it written does not validate the form, it sets up the validator on the form.


Note: I think the API documentation is located here: http://bv.doc.javake.cn/api/


Update after your comment about making an ajax call:

If you want to make an ajax call when the button is clicked, but only if the form is valid, I believe you would do the following:

    $("#btnUpdateModerator").click(function () {
        $('#Users').bootstrapValidator('validate');
        if ($('#Users').bootstrapValidator('isValid')) {
            // Make the ajax call here.
        }
    });

Another way to write this is:

    $("#btnUpdateModerator").click(function () {
        var validator = $('#Users').data('bootstrapValidator');
        validator.validate();
        if (validator.isValid()) {
            // Make the ajax call here.
        }
    });
like image 123
John S Avatar answered Oct 11 '22 00:10

John S


I believe bootstrap validator binds itself to a form's submit() method, so moving it into a function won't actually help you. You can intercept the form's submit method though, and not allow it to submit until you want it to.

$("#myForm").submit(function(e){
    e.preventDefault(); // <-- prevents the form from submitting
    // do stuff
    this.submit() // <-- send it through
});
like image 1
Mike S. Avatar answered Oct 11 '22 01:10

Mike S.