Skip to main content
Kinetic Community

Validation on Blur

Instructions on how to get Kinetic Request CE to check field validations on blur (leaving a field) as well as the standard submit validation.

Usage

It may be necessary or desired to check the validations of a field as soon as the user leaves the field in addition to the standard/ootb validation check at submission time. This is done by leveraging the K('field['+fieldName+']').validate() function and the jQuery blur function set up on the page load against the appropriate object (field).

Note that the below example leverages pattern, but .validate() returns all validation messages for failed validation in an array. They can each/all be displayed, or you can choose to only display the first. It is up to you.

Example

In this example, we will be validating an IP address field, and using notifie library to display and error if the pattern of the field doesn't validate when the user leaves the field, as well as on submit time.

First, the field needs to be set up with the IP address pattern and an appropriate message.

Then the question is given the appropriate Render Attribute so there is a specific class on that field.

And lastly, an on load custom event is set up that validates the question on blur.

$('.ipaddress').blur(function(){
    var fieldName = $(this).attr('name');
    var errors = K('field['+fieldName+']').validate();
    if(errors.length > 0) {
        errors.forEach(function(message){
      $(K('field['+fieldName+']').element()[0]).notifie({ type: 'alert', severity: 'danger', message: message, expire: 500, exitEvents: 'click'});
    })
    }
});