Ali
Ali

Reputation: 267059

How to validate a single field with Hibernate Validator?

I'm looking for a way to validate a single field at a time. This is so, while a user is filling in a form, each time the keypress or blur event occurs for each field, i can validate that field individually and show / hide its error message.

Is that possible? If so, how can I do it? Some google searching hasn't turned up any results which validate a single field, rather than the whole form.

Upvotes: 3

Views: 1438

Answers (2)

Gunnar
Gunnar

Reputation: 18980

You can use javax.validation.Validator#validateProperty() for that purpose.

Upvotes: 4

Luke Schoen
Luke Schoen

Reputation: 4513

I'd recommend that you try using Javascript Regular Expressions to validate user inputs and show/hide error messages. I provided a code implementation example in the post at this link: Example using Regular Expresssions

Add an event listener to relevant input field in the DOM to provide responsiveness.

var letterField = document.getElementById('letterField'); // DOM element containing user input 
letterField.addEventListener('keyup', function() { // key press event listener using anonymous function
    if (event.keyCode === 13) { // run this code if the 'enter' key is pressed
        if ( ... ) { 
            ...
        };
    };
};

If you want to add effects, include the following code to transition the error message. Refer to the jQuery API for other effects:

userInputError.delay(800).fadeOut(); 

Upvotes: -1

Related Questions