WValidator [doc]

A validator is a rule set that validates user input, and can be associated with any WFormWidget.

Validation takes place both at the client side (in the browser) and server side. The advantage of client-side validation is that the user receives feedback without a server round-trip time, but this requires JavaScript support, and this can also easily be tampered with or circumvented. Therefore, server-side validation is always required in any case. All built-in validators provide both client-side and server-side validation. If you implement a custom validator, client-side validation (in JavaScript) may be optional. You could consider to reimplement WRegExpValidator, as it is quite flexible and will give you client-side validation out-of-the-box.

Wt supplies validators for dates, doubles, integers, string length (minimum and maximum) and regular expressions. You can also implement your own server-side/client-side validators.

The example below uses a red background to indicate invalid fields, the default in Wt.

Validator types

WIntValidator: input is mandatory and in range [50 - 100]
WDoubleValidator: range [-5.0 to 15.0]
WDateValidator, default format "yyyy-MM-dd"
WDateValidator, format "dd-MM-yy"
WDateValidator, format "yy-MM-dd", range 1 to 15 October 08
WLengthValidator, 6 to 11 characters
WRegExpValidator, IP address

The IP address validator regexp is: ((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)

All WFormWidgets can have validators, so also the WTextArea. Type up to 50 characters in the box below

Server-side validation

The button below causes the server to validate all input fields above server-side, and puts the state of the validation on the right of every widget:

Valid
data is valid
Invalid
data is invalid
InvalidEmpty
field is empty, but was indicated to be mandatory

Events will be shown here.