Client-Side Validation for Vaadin

The CSValidationTextField (suggestions for a better name are welcome) is an extension of the regular TextField. It can validate the user input with a regular expression and/or a JavaScript program. It can report invalid input with style and error message, or prevent typing invalid input altogether.

Installation

The Client-Side Validation (csvalidation) components require Vaadin 6.2 or later.

Download the csvalidation-<version>.jar and put it in your library folder in your class path, typically in WebContent/WEB-INF/lib.

Notice: The Vaadin Plugin for Eclipse may suggest to rebuild the widget set. You have to answer Yes and build a new widget set if:

Say No if the above conditions do not apply, as this will be a bit easier. Otherwise the plugin will create an unnecessary widget set for your project and compile the widget set already included in the csvalidation.jar unnecessarily.

In this case, the plugin does not automatically update the WebContent/WEB-INF/web.xml descriptor of your project to include the widget set, and you need to add the following yourself:

<servlet>
    ...
    <init-param>
        <description>Application widgetset</description>
        <param-name>widgetset</param-name>
        <param-value>com.vaadin.csvalidation.widgetset.CSValidationWidgetset</param-value>
    </init-param>
</servlet>

If you answer Yes, the plugin will create a new widget set for the project, or use an existing one, and add the following line in its .gwt.xml descriptor:

    <inherits name="com.vaadin.csvalidation.widgetset.CSValidationWidgetset" />

The plugin will then compile the widget set.

Notes

You can't trust the client-side code for validation, so you need to validate the input on the server-side as well. For regular expression validation, you can usually use the same expression for the server-side RegexpValidator (just remember that the regular expressions in Java and JavaScript have slightly different syntax). For example:

// The Finnish Social Security Number
final CSValidatedTextField ssn = new CSValidatedTextField("SSN");
String ssn_regexp = "[0-9]{6}[+-A][0-9]{3}[0-9a-zA-Z]";

// The client-side validation
ssn.setRegExp(ssn_regexp, "000000-000A"); // Must give padding
ssn.setPreventInvalidTyping(true); // Prevent typing invalid values

// The server-side validation
ssn.addValidator(new RegexpValidator(ssn_regexp, "Invalid SSN"));
ssn.setRequired(true);
ssn.setRequiredError("SSN is required");

form.addField("ssn", ssn);

You can validate with JavaScript just as easily:

CSValidatedTextField postcode = new CSValidatedTextField("Postal Code");
postcode.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\";");
layout.addComponent(postcode);

The server-side validation needs separate Java code though, unless you use some JavaScript execution library.

See the example source codes (link above) for more examples.

Included are also handy interactive editors for the regular expressions and JavaScript validators, as you can see in the demo.

Issues

Change Log

Version 0.3.2

Version 0.3.1

Version 0.3.0

License

This software is licensed under the Apache 2 License.