← Back


Client-side validation of text fields




The CSValidator component extension allows client-side validation of TextField, PasswordField, and TextArea components. The field value is validated after each key press without any client-server communication.

You have two options for validating the values: regular expressions and JavaScript. The JavaScript return value can also be used to create counters in text fields and areas. The library comes with special JavaScriptEditor and RegExpEditor components, which allow you to develop and test JavaScript and regular expression validators easily. The editors are also available in the demo.

The add-on is installed as a JAR, which includes the JavaDoc for the server-side API. You need to compile the widget set.

The version 0.5 and later support Vaadin 7, and come as a component extension, which you can attach to a text field with extend(). For Vaadin 6 compatibility, use version 0.4.x, which offers three components instead of an extension: CSValidatedTextField, CSValidatedPasswordField, and CSValidatedTextArea. The functionality is exactly the same.

The add-on should be considered as beta, as compatibility is not tested with IE, but it probably works.

Sample code

final TextField postcode = new TextField("Postal Code");

final CSValidator validator = new CSValidator();
validator.setJavaScript("if (value >= 0 && value < 10000)" +
                       "    \"partial\";" +
                       "else if (value >= 10000 && value <= 99999)" +
                       "    null;" +
                       "else" +
                       "    \"Postal Code must be a 5-digit number between 10000 and 99999\";");

// Validate full name both on client- and server-side.
TextField fn = new TextField("Full Name");

// The regular expression for validation
String fn_regexp = "\\w+ \\w+";

// Validate the on client-side
final CSValidator validator = new CSValidator();
validator.setErrorMessage("Full name must be the first name and the last name");

// Validate the on server-side using the same regular expression
fn.addValidator(new RegexpValidator(fn_regexp, "Invalid full name"));
fn.setRequiredError("Full name is required");


(Loading compatibility data...)

Was this helpful? Need more help?
Leave a comment or a question below. You can also join the chat on Discord or ask questions on StackOverflow.


New CSValidatedTextArea component for multi-line text area. Setting setRows() does not make the regular CSValidatedTextField multi-line, but you have to use this class.

New "valid" and "partial" keywords for JavaScript evaluation results that allow specifying a message after the keyword. The valid and partial messages are displayed in the same area as the error messages. This makes creation of various counters, for example, very easy.

Validation is now done also for empty fields. This may change the behavior of existing applications.

Apache License 2.0


Vaadin 6.3+
Vaadin 6.0+ in 0.2.1
Vaadin 6.2+ in 0.2.2
Vaadin 6.4+ in 0.3.4
Vaadin 6.5+ in 0.4.0
Vaadin 7.0+ in 0.5.0

Vaadin Add-on Directory

Find open-source widgets, add-ons, themes, and integrations for your Vaadin application. Vaadin Add-on Directory
The channel for finding, promoting, and distributing Vaadin add-ons.