Validating jtextfield

05 Nov

It’s in the text components that you first get an inkling of a clear separation between the M and VC parts of the MVC architecture.

The model for text components is an object called a method; fonts are discussed in Chapter 20.

Then we select the “Validate” tab to see the validation options: The default is that the field will not get validated.

For numeric fields, there is a convenient way to validate a value range, but we want to select to run a custom validation script.

This validation can be done in several ways, including manually inserting conditionals every time any input is received, but this process can be tedious and difficult to maintain.

To make data validation simpler, GWT supports JSR-303 Bean Validation, which provides a framework for specifying “constraints” that define what data is valid or allowed for your application.

validating jtextfield-82

This includes methods for setting and retrieving the displayed text, specifying whether the text is “editable” or read-only, manipulating the cursor position within the text, and manipulating text selections.

For example, by specifying the following annotations on your class: You can ensure these conditions are met, as well as generate meaningful errors when they are not: At compile time GWT validation creates a Validator for all your objects that can be used to do client-side validation.

If you are using a Java-based server the very same constraints can be used to do server-side data validation as well.

After the “Edit” button is clicked, a new window will open that allows us to edit the new script: This script also includes a check for an empty string, so that the user can wipe out a wrong string and start from scratch.

As I mentioned before, information is passed to the validation function in the event object, and in the code we see that the member ‘value’ is used to communicate the current value of the field.