i need to make a validation only if a modal is open, because if i open it, and then i close it, and the i press the button that opens the modal it doesn't work because it is making the jquery validation, but not showing because the modal was dismissed.
So i want to ad a jquery if modal is open so the i do validate, is this possible?
<script>
$(document).ready(function(){
var validator =$('#form1').validate(
{
ignore: "",
rules: {
usu_login: {
required: true
},
usu_password: {
required: true
},
usu_email: {
required: true
},
usu_nombre1: {
required: true
},
usu_apellido1: {
required: true
},
usu_fecha_nac: {
required: true
},
usu_cedula: {
required: true
},
usu_telefono1: {
required: true
},
rol_id: {
required: true
},
dependencia_id: {
required: true
},
},
highlight: function(element) {
$(element).closest('.grupo').addClass('has-error');
if($(".tab-content").find("div.tab-pane.active:has(div.has-error)").length == 0)
{
$(".tab-content").find("div.tab-pane:hidden:has(div.has-error)").each(function(index, tab)
{
var id = $(tab).attr("id");
$('a[href="#' + id + '"]').tab('show');
});
}
},
unhighlight: function(element) {
$(element).closest('.grupo').removeClass('has-error');
}
});
}); // end document.ready
</script>
You can also directly use jQuery.
won't work if the modal hasn't been shown before. You will need to add an extra condition:
so the answer taking into account first appearance:
On bootstrap-modal.js v2.2.0:
You can use
Bootstrap adds the
in
class when the modal is open and removes it when closedCheck if a modal is open
$('.modal:visible').length && $('body').hasClass('modal-open')
To attach an event listener
To avoid the race condition @GregPettit mentions, one can use:
as discussed in Twitter Bootstrap Modal - IsShown.
When the modal is not yet opened,
.data('bs.modal')
returnsundefined
, hence the|| {}
- which will makeisShown
the (falsy) valueundefined
. If you're into strictness one could do($("element").data('bs.modal') || {isShown: false}).isShown