GMan
GMan

Reputation: 464

Validation in bootstrap modal dialog

I am using Bootstrap 2.

Inside a form I have a button which opens a bootstrap modal dialog. Within the bootstrap modal dialog I have a select2 control, which I want to validate. i.e. I want at least one value in the select2 control. How do I validate the select2 control as a required field before the dialog is closed?

Note: closing the dialog doesn't submit the form.

Does Select2 have a mandatory/required property? Can the Save button of the dialog be linked to checking the select2 control?

I am using MVC4, bootstrap 2, razor.

Upvotes: 2

Views: 3810

Answers (1)

Feugy
Feugy

Reputation: 1918

Bootstrap modal component fire a hide event when closing. You should register an handler to this event, make your validation, and prevent bubbling if needed.

For example:

$('#myModal').on('hide', function (evt) {
  var isValid = // make your validation
  if (!isValid) {
    event.preventDefault();
    event.stopImmediatePropagation();
    return false;
  }
});

Upvotes: 2

Related Questions