The plugin only provides error message option which is shown when the field is invalid. Is it possible to add a valid message option shown if the field is valid?
The quick answer is YES. This example shows you how to implement this feature.
Assuming that our form has two text boxes which are username and password.
As usual, the plugin is called by following snippet code:
The implementation idea consists of four steps as following.
Adding valid message option
I add an option named validMessage for each field. As the name suggests, the plugin will show the valid message when the field is valid.
Creating an element that shows the invalid message
Basically, you can place the valid message anywhere you want as long as you can retrieve it later.
Behind the scenes, FormValidation places each error message in a span element. The field error messages are placed inside an element which can be retrieved by $field.data('bv.messages').
To make the example easily to follow, I simply create a span element right after each field for showing the valid message:
In fact, the number of fields might be greater than two as in our example. Therefore, it is much better if these valid message elements are created dynamically. It's quite easy to do it by triggering the init.field.fv event:
The init.field.fv event is triggered after the field is initialized by the plugin. In order to trigger the event, you must declare .on('init.field.fv') before calling formValidation(options).
In step 2 above, the custom option validMessage is determined by using the getOptions() method.