By default, all messages are shown at the same time. It's also possible if you want only one
message to be shown each time by triggering the err.validator.fv event.
This event is triggered when the field doesn't pass a particular validator.
In the following registration form, the username field must pass three validators. But there
is one message shown each time.
< form id = "registrationForm" method = "post" class = "form-horizontal" >
< div class = "form-group" >
< label class = "col-xs-3 control-label" > Username</ label >
< div class = "col-xs-4" >
< input type = "text" class = "form-control" name = "username" />
</ div >
</ div >
< div class = "form-group" >
< div class = "col-xs-5 col-xs-offset-3" >
< button type = "submit" class = "btn btn-default" > Validate</ button >
</ div >
</ div >
</ form >
< script >
$ ( document ). ready ( function () {
$ ( '#registrationForm' )
. formValidation ({
framework : 'bootstrap' ,
icon : {
valid : 'glyphicon glyphicon-ok' ,
invalid : 'glyphicon glyphicon-remove' ,
validating : 'glyphicon glyphicon-refresh'
},
fields : {
username : {
validators : {
notEmpty : {
message : 'The username is required and cannot be empty'
},
stringLength : {
min : 6 ,
max : 30 ,
message : 'The username must be more than 6 and less than 30 characters long'
},
regexp : {
regexp : /^[a-zA-Z0-9]+$/ ,
message : 'The username can only consist of alphabetical and digits'
}
}
}
}
})
. on ( 'err.validator.fv' , function ( e , data ) {
// $(e.target) --> The field element
// data.fv --> The FormValidation instance
// data.field --> The field name
// data.element --> The field element
// data.validator --> The current validator name
data . element
. data ( 'fv.messages' )
// Hide all the messages
. find ( '.help-block[data-fv-for="' + data . field + '"]' ). hide ()
// Show only message associated with current validator
. filter ( '[data-fv-validator="' + data . validator + '"]' ). show ();
});
});
</ script >